简体   繁体   中英

Cannot pass NSString to a NSURL URLWithString

Try to pass my NSString to a URLWithString but no url appear:

//here i get my url from my API and replacing tags
NSString *queryContent = [[[(webLinks)[@"content"] stringByReplacingOccurrencesOfString:@"&" withString:@"&"]
                                                   stringByReplacingOccurrencesOfString:@"<p>" withString:@""]
                                                   stringByReplacingOccurrencesOfString:@"</p>" withString:@""];
//here i get the full url 
NSURL * url = [NSURL URLWithString:queryContent];

Adding a NSLogs:

 NSLog(@"query:%@", queryContent);
 NSLog(@"website:%@", url);

and the result is this:

query:http://mywebsite.com
website:(null)

Whats wrong?

thanks

//SOLVED//

here the correct code if any of you use my same system JSON API for WordPress:

NSRange r;
NSString *queryUrl = [(webLinks)[@"content"] stringByReplacingOccurrencesOfString:@"\n" withString:@""];
NSString *website = queryUrl;
while ((r = [website rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
    website = [website stringByReplacingCharactersInRange:r withString:@""];

NSURL * url = [NSURL URLWithString:website];
[webView loadRequest:[NSURLRequest requestWithURL:url]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM