- [NSNull length]:发送到JSON对象的无法识别的选择器 [英] -[NSNull length]: unrecognized selector sent to JSON objects

查看:215
本文介绍了 - [NSNull length]:发送到JSON对象的无法识别的选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用最新的SDK开发iOS 5.0+应用。

I'm developing an iOS 5.0+ app with latest SDK.

我的代码出现了一个非常奇怪的错误:

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

这是我的日志:

???????????? <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'

似乎 authenticationToken NULL 。但是我不明白,如果 authenticationToken NULL 为什么我看不到日志上的NULL AUTHTOKEN

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.

我第二次运行该方法时出现此错误,第一次,我没有得到任何错误。这是我的日志:

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

顺便说一下:

NSString* authenticationToken;

有什么建议吗?

在某处可能有内存泄漏 ...

Maybe there is a Memory Leak somewhere...

推荐答案

<我对JSON解释器使用NSNull这种疯狂使用的解决方案是在NSNull上创建一个类别,我在其中定义integerValue,floatValue,length等等 - 为所有人返回0。每当您遇到另一次崩溃时,请添加一个新类别。当我遇到这个问题时,我认为我有6或7个。

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.

不执行此操作的问题是你必须在转换的对象中找到NULL - 一个PITA在我看来。

The problem with NOT doing this is you have to look for the NULL everywhere in your converted objects - a PITA in my opinion.

编辑:我正在使用的代码,全部在NSNull + JSON.m文件中:

the code I'm using, all in a NSNull+JSON.m file:

@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:现在在Swift 3中:

Now in 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 }
}

这篇关于 - [NSNull length]:发送到JSON对象的无法识别的选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆