0

While I usually love my code to succeed, today I need to test it's failure. Using swift2.3, need a string that I could pass to

dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true) 

and have it fail. I assume it can fail because it returns a optional.

Rob Bonner
  • 9,276
  • 8
  • 35
  • 55
  • A conversion to NSUnicodeStringEncoding cannot fail. – Martin R Oct 31 '17 at 12:49
  • I updated the question. dataUsingEncoding returns an optional, why would that be if it always succeeds? – Rob Bonner Oct 31 '17 at 12:53
  • 1
    It depends on the encoding, compare https://stackoverflow.com/questions/46152617/can-the-conversion-of-a-string-to-data-with-utf-8-encoding-ever-fail. A conversion to "ISO Latin" or "Mac OS Roman" can fail because those encodings cannot represent all possible Unicode characters. – Martin R Oct 31 '17 at 13:05

0 Answers0