无法将NSString传递给NSURL URLWithString

时间:2015-02-05 11:27:36

标签: ios objective-c xcode

尝试将NSString传递给URLWithString,但不会显示网址:

//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];

添加NSLogs:

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

结果如下:

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

怎么了?

感谢

1 个答案:

答案 0 :(得分:1)

<强> // //解决

这里是正确的代码,如果你们中的任何人使用我的相同的WordPress系统JSON API:

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]];