When Swift originally launched, NSString (older iOS strings) and native Swift strings were completely interchangeable, as were NSArray and Swift arrays, plus NSDictionary and Swift dictionaries. This got changed in Swift 1.2 so that you need to explicitly cast between these data types, and this remains the same in Swift today.
So, to cast between Swift strings and NSString, you need to do a simple typecast like this:
let str = "Hello" let otherStr = str as NSString
Note that you don't need to force the typecast because the two data types are still interoperable.
Available from iOS 7.0
Did this solution work for you? Please pass it on!
Other people are reading…
About the Swift Knowledge Base
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Download for free!
Want a free 75-minute video teaching functional programming, protocol-oriented programming, and more? This is your lucky day!