ios -[NSNull 长度]:无法识别的选择器发送到 JSON 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16607960/
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
-[NSNull length]: unrecognized selector sent to JSON objects
提问by VansFannel
I'm developing an iOS 5.0+ app with latest SDK.
我正在使用最新的 SDK 开发 iOS 5.0+ 应用程序。
I get a very strange error with this code:
这段代码出现了一个非常奇怪的错误:
- (NSMutableURLRequest*)setupRequestWithService:(NSString*)service andMethod:(NSString*)method
{
NSString* url = [NSString stringWithFormat:@"%@%@.svc/%@", serverUrl, service, method];
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
// Set authentication token.
NSLog(@"???????????? %@", authenticationToken);
if (authenticationToken == nil)
NSLog(@"NULL AUTHTOKEN");
if ([authenticationToken isEqual:[NSNull null]])
NSLog(@"NSNULL AUTHTOKEN");
if (request == nil)
NSLog(@"NULL REQUEST");
[request addValue:authenticationToken forHTTPHeaderField:REQUEST_HEADER_AUTH_TOKEN];
return request;
}
This is my log:
这是我的日志:
???????????? <null>
NSNULL AUTHTOKEN
-[NSNull length]: unrecognized selector sent to instance 0x3b5a5090
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized selector sent to instance 0x3b5a5090'
It seems that authenticationToken
is NULL
. But I don't understand that, if authenticationToken
is NULL
why I don't see NULL AUTHTOKEN
on the log.
看来authenticationToken
是NULL
。但我不明白,如果authenticationToken
这NULL
就是我没有NULL AUTHTOKEN
在日志上看到的原因。
I get this error the second time I run that method, the first time, I don't get any error. This is my log:
我第二次运行该方法时出现此错误,第一次没有出现任何错误。这是我的日志:
???????????? (null)
NULL AUTHTOKEN
By the way:
顺便一提:
NSString* authenticationToken;
Any advice?
有什么建议吗?
Maybe there is a Memory Leak
somewhere...
也许Memory Leak
某处...
回答by David H
My solution to this maddening use of NSNull by JSON interpreters is to create a category on NSNull, where I define integerValue, floatValue, length, etc - return 0 for all. Everytime you get another crash add a new category. I think I had 6 or 7 when I had this issue.
我对 JSON 解释器对 NSNull 的这种疯狂使用的解决方案是在 NSNull 上创建一个类别,我在其中定义 integerValue、floatValue、length 等 - 全部返回 0。每次遇到另一次崩溃时,请添加一个新类别。当我遇到这个问题时,我想我有 6 或 7 个。
The problem with NOT doing this is you have to look for the NULL everywhere in your converted objects - a PITA in my opinion.
不这样做的问题是您必须在转换后的对象中到处寻找 NULL - 在我看来是 PITA。
EDIT: the code I'm using, all in a NSNull+JSON.m file:
编辑:我正在使用的代码,都在一个 NSNull+JSON.m 文件中:
@interface NSNull (JSON)
@end
@implementation NSNull (JSON)
- (NSUInteger)length { return 0; }
- (NSInteger)integerValue { return 0; };
- (float)floatValue { return 0; };
- (NSString *)description { return @"0(NSNull)"; }
- (NSArray *)componentsSeparatedByString:(NSString *)separator { return @[]; }
- (id)objectForKey:(id)key { return nil; }
- (BOOL)boolValue { return NO; }
@end
EDIT2: Now in Swift 3:
EDIT2:现在在 Swift 3 中:
extension NSNull {
func length() -> Int { return 0 }
func integerValue() -> Int { return 0 }
func floatValue() -> Float { return 0 };
open override var description: String { return "0(NSNull)" }
func componentsSeparatedByString(separator: String) -> [AnyObject] { return [AnyObject]() }
func objectForKey(key: AnyObject) -> AnyObject? { return nil }
func boolValue() -> Bool { return false }
}
回答by Mar0ux
The error message is pretty clear. NSNull
and nil
are different things:
错误信息非常清楚。NSNull
并且nil
是不同的事情:
The NSNull class defines a singleton object used to represent null values in
collection objects (which don't allow nil values).
If you want to check if authenticationToken
is NSNull
try: [authenticationToken isEqual: [NSNull null]]
如果你想检查是否authenticationToken
是NSNull
尝试:[authenticationToken isEqual: [NSNull null]]
回答by Eric Baker
In line with David H's answer, how about a category on NSNull
that just uses ObjC's message forwarding to "do nothing", to emulate the runtime's behavior when sending messages to nil
?
与 David H 的回答一致,NSNull
仅使用 ObjC 的消息转发“什么都不做”的类别如何在向 发送消息时模拟运行时的行为nil
?
Like this:
像这样:
@interface NSNull (ForwardInvocation)
@end
@implementation NSNull (ForwardInvocation)
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
return [NSNull methodSignatureForSelector:@selector(description)];
}
- (void)forwardInvocation:(NSInvocation *)anInvocation {
// do nothing; prevent 'unrecognized selector' crashes
}
@end
The [NSNull methodSignatureForSelector:@selector(description)];
takes advantage of the fact that NSNull
inherits from NSObject
, which provides the description
method. This satisfies the forwarding mechanism requirement for implementing -methodSignatureForSelector:
.
的[NSNull methodSignatureForSelector:@selector(description)];
利用NSNull
继承自的事实NSObject
,它提供了description
方法。这满足了实现的转发机制要求-methodSignatureForSelector:
。
回答by Daniel C.
The problem comes because your method return an NSNull object.
You can't check [authenticationToken isEqual:[NSNull null]])
because [NSNull null]
give an instance of an object. So it's different from your object itself.
If you want to check if you received an NSNull object you need to check like this: [authenticationToken isKindOfClass:[NSNull class]]
instead.
问题出现是因为您的方法返回一个 NSNull 对象。你不能检查,[authenticationToken isEqual:[NSNull null]])
因为[NSNull null]
给了一个对象的实例。所以它与你的对象本身不同。如果你想检查你是否收到了一个 NSNull 对象,你需要像这样检查:[authenticationToken isKindOfClass:[NSNull class]]
相反。