Vi Ad
Vi Ad

Reputation: 43

How to covert this objective c into swift

NSURL *url = [NSURL URLWithString:@"http://iminichrispy.com/Xcode/TextFromWeb.txt"];
    NSString *content = [NSString stringWithContentsOfURL:url encoding:NSStringEncodingConversionAllowLossy error:nil];
    label.text = content;

I have tried the following

var url:NSURL = NSURL(string: "http://iminichrispy.com/Xcode/TextFromWeb.txt")

var content:NSString = NSString(contentsOfURL: url, encoding: NSStringEncodingDetectionAllowLossyKey, error: nil)

label.text = content

At nil I keep getting the error: "Extra argument 'error' in call"

Upvotes: 2

Views: 909

Answers (2)

newacct
newacct

Reputation: 122419

The encoding parameter is supposed to be an encoding (which is an integer), not conversion option (which is a string, so the types don't match):

var url:NSURL? = NSURL(string: "http://iminichrispy.com/Xcode/TextFromWeb.txt")

var content:NSString? = NSString(contentsOfURL: url!,
                                 encoding: NSUTF8StringEncoding,
                                 error: nil)

Upvotes: 3

ares777
ares777

Reputation: 3628

Use something like this:

       var e : NSError?
   let content : NSString = NSString(contentsOfURL:url, encoding:NSUTF8StringEncoding, error:&e)

nil value is not supposed to be there.

Upvotes: 0

Related Questions