ios - Using a Swift String with NSData without NSString -
is possible init swift string (not nsstring) contents of nsdata object without creating nsstring first?
i know can use nsstring:
var datastring = nsstring(data data: nsdata!, encoding encoding: uint) but how can use basic swift string type? thought swift strings , nsstrings interchangeable, have data out of nsdata using nsstring , assign value swift string?
as of swift 1.2 aren't quite interchangeable, convertible, there's no reason not use nsstring , constructors when need to. work fine:
var datastring = nsstring(data:data, encoding:nsutf8stringencoding) as! string the as! needed because nsstring(...) can return nil invalid input - if aren't sure data represents valid utf8 string, may wish use following instead return string? (aka optional<string>).
var datastring = nsstring(data:data, encoding:nsutf8stringencoding) string? once constructed, can use datastring other swift string, e.g.
var foo = datastring + "some other string" 
Comments
Post a Comment