Using a Swift String with NSData without NSString
As of Swift 1.2 they aren't quite interchangeable, but they are convertible, so there's really no reason not to use
NSString and its constructors when you need to. This will work fine:
var datastring = NSString(data:data, encoding:NSUTF8StringEncoding) as! String
as! is needed because
NSString(...) can return
nil for invalid input - if you aren't sure that the data represents a valid UTF8 string, you may wish to use the following instead to return a
var datastring = NSString(data:data, encoding:NSUTF8StringEncoding) as String?
Once constructed, you can then use
datastring just like any other Swift string, e.g.
var foo = datastring + "some other string"