“…you should almost never need to use the NSString class directly in your own code”
Excerpt From: Apple Inc. “Using Swift with Cocoa and Objective-C.” iBooks. https://itun.es/us/1u3-0.l
Despite this bold statement, I think I have found a situation where I need an NSString. For instance, consider this code block:
NSString(
contentsOfURL: NSURL(string: "http://api.stackexchange.com/answers?site=stackoverflow"),
encoding: NSUTF8StringEncoding,
error: &error)
I can't do that with a Swift string, right? Either way, this doesn't work:
String(
contentsOfURL: NSURL(string: "http://api.stackexchange.com/answers?site=stackoverflow"),
encoding: NSUTF8StringEncoding,
error: &error) //Compiler error
So should I just use NSString here? Or is there another preferred way to do this in Swift?
NSString +contentsOfURL:..., because it makes a synchronous networking request. UseNSURLSession dataTaskWithURL:completionHandler:instead.