AFNetworking - 无效参数不满足:url

arl*_*rlg 17 ios afnetworking

我刚刚在ios7上尝试了AFNetworking,我收到了这个错误:

    /Classes/AFHTTPClient.m:227
 2013-09-16 18:25:57.557 App[13531:a0b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: url
Run Code Online (Sandbox Code Playgroud)

我不知道发生了什么,lib和ios7有问题吗?谢谢.

Kas*_*nck 26

正如莱昂在评论中所说,评论NSParameterAssert不是一个理想的解决方案.作者之所以提出断言是有原因的AFNetworking.未传递断言的代码可能是由无效的URL引起的.

请记住,NSURL工厂方法(即+ URLWithString:它的兄弟姐妹)将nil在传递无效的URL字符串时返回.在此上下文中,无效字符串是包含无效URL的字符串.

您应该做什么而不是评论我们的断言,确保您没有将任何无效的URL传递给您的AFHTTPClient实例.此Stackoverflow答案提供了如何验证URL的示例.以下是答案中的示例:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用添加比例逃脱NSStringstringByAddingPercentEscapesUsingEncoding方法.像这样:

NSString *encoded = [notEncoded stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Run Code Online (Sandbox Code Playgroud)


Jam*_*ter 5

我刚遇到这个错误,原因是网址开头的空格:

 http://host.com/etc
^
Run Code Online (Sandbox Code Playgroud)