Vadim Nikolaev Vadim Nikolaev - 2 months ago 30
Swift Question

Error after converting to Swift 3.0

After I converted my project to Swift 3.0, I find error in log from variable:

var arrayOfHours = stringArray.map {String(describing: $0!.characters.prefix(2))}


Error is:

code of error: "Optional(Swift.String.CharacterView(_core: Swift._StringCore(_baseAddress: Optional(0x000060800024ee70), _countAndFlags: 2, _owner: Optional(Swift._HeapBufferStorage<Swift._StringBufferIVars,
Swift.UInt16>))))"


where is my error occuring?

Update:

var stringArray in console: [Optional("1226"), Optional("1249"), Optional("1312"), Optional("1336"), Optional("1359"), Optional("1422"), Optional("1446"), Optional("1509"), Optional("1532"), Optional("1549"), Optional("1607"), Optional("1624"), Optional("1642"), Optional("1659"), Optional("1717"), Optional("1734"), Optional("1752"), Optional("1809"), Optional("1827"), Optional("1844"), Optional("1902"), Optional("1919"), Optional("1954"), Optional("2032"), Optional("2107"), Optional("2142"), Optional("2217"), Optional("2252"), Optional("2327"), Optional("2402"), Optional("2437"), Optional("2512")]

Answer
var stringArray: [String?] = ["1226", "1249"]
print(stringArray) // [Optional("1226"), Optional("1249")]

var arrayOfHours = stringArray.map { String($0!.characters.prefix(2)) }
print(arrayOfHours) // ["12", "12"]
Comments