Swift version: 5.6
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.
SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!
Available from iOS 7.0
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.