ios AFNetworking - 无效参数不满足:url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18832995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:15:16  来源:igfitidea点击:

AFNetworking - Invalid parameter not satisfying: url

iosafnetworking

提问by arlg

I just tried AFNetworking on ios7 and i get this error:

我刚刚在 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

I don't know what's going on, is there an issue with the lib an ios7 ? thanks.

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

回答by Kasper Munck

As Leon says in his comment, commenting out NSParameterAssertis not an ideal solution. An assertion has been put there for a reason by the author of AFNetworking. The code not passing the assertion is likely to be caused by an invalid URL.

正如 Leon 在评论中所说,注释掉NSParameterAssert并不是一个理想的解决方案。的作者出于某种原因在那里提出了一个断言AFNetworking。不通过断言的代码很可能是由无效的 URL 引起的。

Remember that the NSURLfactory methods (that is, + URLWithString:and its siblings) will return nilwhen they're passed an invalid URL string. In this context, an invalid string is a string containing an invalid URL.

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

What you should do instead of commenting our the assertion, is making sure that you are not passing any invalid URL's to your AFHTTPClientinstance. This Stackoverflow answersgives an example of how you could validate a URL. Here's a sample from the answer:

您应该做的而不是评论我们的断言,是确保您没有将任何无效的 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];
}

Alternatively, you could add percentage escapes using NSString's stringByAddingPercentEscapesUsingEncodingmethod. Like this:

或者,您可以使用NSString'sstringByAddingPercentEscapesUsingEncoding方法添加百分比转义。像这样:

NSString *encoded = [notEncoded stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

回答by James Webster

I just had this error, the cause was a space at the start of the url:

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

 http://host.com/etc
^

回答by Amr Angry

as Kasper says --> "You could add percentage escapes using NSString's stringByAddingPercentEscapesUsingEncoding method"

正如 Kasper 所说-->“您可以使用 NSString 的 stringByAddingPercentEscapesUsingEncoding 方法添加百分比转义”

or better use the following since it deprecated in ios 9

或者更好地使用以下内容,因为它在 ios 9 中已弃用

path = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

回答by ArturOlszak

I had a weird case today with this assert. My problem was that I have copied url from other text editor (Sublime) and that's why url was invalid.

今天我遇到了一个关于这个断言的奇怪案例。我的问题是我从其他文本编辑器(Sublime)复制了 url,这就是 url 无效的原因。

I was not believing until I have tested it few times.

直到我测试了几次,我才相信。

回答by yogesh wadhwa

Hope its Working.. In AFnetworing class name AFURLRequestSerialization.m

希望它的工作.. 在 AFnetworking 类名 AFURLRequestSerialization.m

commented the line 227 : // NSParameterAssert(url);