user1548284
user1548284

Reputation: 13

NSString initWithContentsOfURL problems

I am updating an iOS app to be a good boy and clean out some deprecated methods. I have been using NSString initWithContentsOfURL and I am trying to implement the new version initWithContentsOfURL:usedEncoding:error:.

I have used the following in my view controller:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSURL *url = [[NSURL alloc] initWithString:@"http://myurl.com/directory/file.txt"];
    NSError *error;
    NSStringEncoding *encoding = NULL;
    NSString *tryString = [[NSString alloc] initWithContentsOfURL:url
                                                 usedEncoding:encoding
                                                        error:NULL];
    NSLog(@"string:%@", tryString);
    [super viewDidLoad];
}

But I keep getting a thread break at the error:NULL point. I have tried error:nil and using a pointer to the predefined error object as nil or NULL but same thing happens. Has anyone any ideas or tips.

Upvotes: 0

Views: 4006

Answers (2)

Rui Peres
Rui Peres

Reputation: 25917

Both the error and encoding should be pass like this:

NSError *error = nil;
NSStringEncoding encoding = nil;

NSString *tryString = [[NSString alloc] initWithContentsOfURL:url
                                                 usedEncoding:&encoding
                                                        error:&error];

Upvotes: 1

Monolo
Monolo

Reputation: 18253

NSError *error = nil;
NSStringEncoding encoding = 0;

NSString *tryString = [[NSString alloc] initWithContentsOfURL:url
                                                 usedEncoding:&encoding
                                                        error:&error];

Upvotes: 2

Related Questions