我正在尝试检查我将用作 URL 的字符串是否以 http 开头。我现在尝试检查的方式似乎不起作用。这是我的代码:
NSMutableString *temp = [[NSMutableString alloc] initWithString:@"http://"];
if ([businessWebsite rangeOfString:@"http"].location == NSNotFound){
NSString *temp2 = [[NSString alloc] init];
temp2 = businessWebsite;
[temp appendString:temp2];
businessWebsite = temp2;
NSLog(@"Updated BusinessWebsite is: %@", businessWebsite);
}
[web setBusinessWebsiteUrl:businessWebsite];
有什么想法吗?
最佳答案
试试这个:if ([myString hasPrefix:@"http"])
.
顺便说一句,您的测试应该是 != NSNotFound
而不是 == NSNotFound
。但是假设您的 URL 是 ftp://my_http_host.com/thing
,它会匹配但不应该匹配。
https://stackoverflow.com/questions/7934688/
相关文章:
objective-c - xcode中Base SDK、iOS部署目标、Target、Projec
objective-c - NSAttributedString 的 boundingRectWit
objective-c - 在objective-c中YES/NO,TRUE/FALSE和true/
objective-c - 使用 Objective-C 在 iOS 9 中将状态栏文本颜色更改为浅
objective-c - Objective-C 中的 "unrecognized selecto