是否有任何 API 或良好的函数可以将“www.example.com”或“example.com”等字符串格式化为“http://www.example.com”等有效的 urlstrings?
+ (NSString*)complete:(NSString *)urlString
{
NSArray * urlParts = [urlString componentsSeparatedByString:@"."];
if(urlParts.count==3)
return [NSString stringWithFormat:@"http://www.%@.%@",[urlParts objectAtIndex:1],[urlParts objectAtIndex:2]];
else if(urlParts.count==2)
return [NSString stringWithFormat:@"http://www.%@.%@",[urlParts objectAtIndex:0],[urlParts objectAtIndex:1]];
return nil;
}
这就是当前解决方案的样子,但这对我来说看起来不是很好和可靠。