ios - 无法将 NSString 传递给 NSURL URLWithString

标签 ios objective-c xcode

尝试将我的 NSString 传递给 URLWithString 但没有出现 url:

//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)

怎么了?

谢谢

最佳答案

//已解决//

这里是正确的代码,如果你们中有人使用我的 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]];

关于ios - 无法将 NSString 传递给 NSURL URLWithString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28342757/

相关文章:

ios - 在 ionic 上删除和添加平台后出现问题

objective-c - 如何阻止 UIPanGestureRecognizer 识别点击

iphone - 为什么当输入的文本达到键盘水平时 UITextView 不滚动?

c++ - C++ Unordered_set 函数中的溢出

ios - 无法为 TableView Controller (UITableViewController) 选择自定义类

ios - 标签显示超出我的自定义 TableViewCell 范围并通过 swift 设置自动布局

ios - 快速将 UIFont 转换为 CGFont?

ios - .scaleAspectFit - 用黑色填充空白

iOS AdMob : Error No Ad to Show

ios - 如何使用 iPhone 键盘上的 "return"键触发 unwind segue?