本文介绍了AFNetworking - 无效参数不满足:url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

    /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

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

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

推荐答案

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

As Leon says in his comment, commenting out NSParameterAssert is 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.

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

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

你应该做什么而不是评论我们的断言,确保你没有将任何无效的URL传递给你的 AFHTTPClient 实例。 提供了如何验证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 AFHTTPClient instance. This Stackoverflow answers gives an example of how you could validate a URL. Here's a sample from the answer:

- (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];
}

或者,您可以使用 NSString 's stringByAddingPercentEscapesUsingEncoding 方法。像这样:

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

NSString *encoded = [notEncoded stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

这篇关于AFNetworking - 无效参数不满足:url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 01:29