如何在字符串中添加"http://"(如果缺少)

Ant*_*ste 3 objective-c ios

我有一个字符串[currentTweet pic],应该是图片的URL(从XML文件解析).问题是某些链接缺少URL中的"http://",因此某些图片不会出现.如何将"http://"添加到缺少的位置?

任何帮助都非常感谢!谢谢

Jor*_*ers 13

我认为你的意思是NSString在这里,而不是a NSURL.

NSString *picURL = [currentTweet pic];
if (![picURL hasPrefix:@"http://"]) {
    picURL = [@"http://" stringByAppendingString:picURL];
}
Run Code Online (Sandbox Code Playgroud)

不确定推特是否曾用过https图片网址,但你也应该检查一下是否https://确定.