【发布时间】:2015-05-20 06:57:13
【问题描述】:
在我的 Swift iOS 项目中,我想在请求服务器之前检查是否是有效的 url。我之前在 Objective C 代码中检查了许多元素,例如 www、http、https、: 等,以验证 URL 是否正确。我们在 Swift 代码中有类似的东西吗?
我期待这样的 Obj C 方法。
- (BOOL) validateUrl: (NSString *) candidate {
NSString *urlRegEx =
@"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";
NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];
return [urlTest evaluateWithObject:candidate];
}
请提出建议。
【问题讨论】:
-
你之前是做什么的?你有没有尝试做同样的事情?
-
我尝试使用 NSPredicate predicateWithFormat,但在 Swift 中无法实现。
-
嗨,我需要验证这个 URL -> h t t p://192.168.1.146:8081/Myservelet/WebserviceA/?username=stella
-
请记住勾选解决您问题的答案:-)