ios [NSNull isEqualToString:]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6376492/
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-30 20:23:45  来源:igfitidea点击:

[NSNull isEqualToString:]

iphoneiosstringdictionarynull

提问by Sam Baumgarten

Im trying to set a string to "No Display name if [object objectForKey@"display_name"] is NULL". It crashing with this in the log

我试图将字符串设置为“如果 [object objectForKey@”display_name”] 为 NULL,则不显示名称”。它在日志中崩溃了

2011-06-16 10:58:36.251 BV API[15586:ef03] displayNameType is: NSNull
2011-06-16 10:58:36.251 BV API[15586:ef03] +[NSNull isEqualToString:]: unrecognized selector sent to class 0x1228c40
2011-06-16 10:58:36.253 BV API[15586:ef03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSNull isEqualToString:]: unrecognized selector sent to class 0x1228c40'
*** First throw call stack:
(0x1198f1c 0x132b52e 0x119bb2b 0x10f3076 0x10f2bb2 0x116d9 0x112ad 0x8b0e17 0x8b9783 0x8b43ae 0x8b9d20 0xb6312b 0x815c35 0xac2e1e 0x8b7583 0x8b771d 0x8b775d 0xc1b5 0x7ed84c 0x7ed7e2 0x89477a 0x894c4a 0x893ee4 0x813002 0x81320a 0x7f960c 0x7ecd52 0x211b8f6 0x116831a 0x10c3d07 0x10c1e93 0x10c1750 0x10c1671 0x211a0c3 0x211a188 0x7eac29 0x1b29 0x1aa5)
terminate called throwing an exceptionsharedlibrary apply-load-rules all
Current language:  auto; currently objective-c


NSString *displayNameType = (NSString *)[[object objectForKey:@"display_name"] class ];
NSLog(@"displayNameType is: %@", displayNameType);

NSString *displayNameString = [NSString stringWithFormat:@"%@", [object objectForKey:@"display_name"]];
displayNameString = [displayNameString uppercaseString];
if ([displayNameType isEqualToString:@"NSNull"]) {
    NSLog(@"dnt is null");
    NSString *displayNameString = @"No Display Name";
    displayNameString = [displayNameString uppercaseString];
}

回答by Vincent Guerci

A cast will not change an object class(type).

强制转换不会改变对象class(类型)。

You have to manage the case when your value is [NSNull null]with something like :

当你的价值是这样的时候,你必须管理这个案例[NSNull null]

id displayNameTypeValue = [object objectForKey:@"display_name"];
NSString *displayNameType = @"";
if (displayNameTypeValue != [NSNull null])
   displayNameType = (NSString *)displayNameTypeValue;

回答by Jerry Horton

I created a category on NSNull, works well for me:

我在 NSNull 上创建了一个类别,对我来说效果很好:

@interface NSNull (string)

-(BOOL) isEqualToString:(NSString *) compare;

@end

@implementation NSNull (string)

-(BOOL) isEqualToString:(NSString *) compare {    

    if ([compare isKindOfClass:[NSNull class]] || !compare) {
        NSLog(@"NSNull isKindOfClass called!");
        return YES;
    }

    return NO;  
}
@end

回答by André Moruj?o

You might want something like this:

你可能想要这样的东西:

NSString *displayNameType = NSStringFromClass([[object objectForKey:@"display_name"] class]);

And btw in your question, it shouldn't read "if it's NULL", but rather "if it's an NSNullinstance".

顺便说一句,在你的问题中,它不应该是“如果它是 NULL”,而是“如果它是一个NSNull实例”。

回答by fsaint

I'll throw in my answer to clarify a bit. The problem is that the object in the NSDictionary has [NSNull null] value (slightly different to both nil and NULL). The issue comes from some libraries (a particular JSON parser comes to my mind) set the value for some keys with NULL value to [NSNull null]. Why? Because sometimes it is needed to differentiate in a NSDictionary the case when a key is not present from the case when the key has NULL value. In an NSDictionary there is no way of telling, but JSON structures do convey such difference. When you get a a variable that comes from a library or parser that does that the value may be [NSNull null]. NSNull is a singleton thus just checking for equality (pointer equality) is enough. For example I would do:

我会抛出我的答案来澄清一点。问题是 NSDictionary 中的对象有 [NSNull null] 值(与 nil 和 NULL 都略有不同)。问题来自一些库(我想到了一个特定的 JSON 解析器)将某些具有 NULL 值的键的值设置为 [NSNull null]。为什么?因为有时需要在 NSDictionary 中区分键不存在的情况和键具有 NULL 值的情况。在 NSDictionary 中无法说明,但 JSON 结构确实传达了这种差异。当您获得来自库或解析器的变量时,该值可能是 [NSNull null]。NSNull 是一个单例,因此只检查相等(指针相等)就足够了。例如我会这样做:

NSString *value = [object objectForKey:@"display_name"];
if (value && value != [NSNull null]){
   // Here I have the value I expect

   // Do something
}else{
   // The value is null ... don't display
}

回答by QuangLoc

To my experience, the font may be well missing. If you got the problem, you could check where you set the new font and if it exists.

根据我的经验,字体可能会丢失。如果遇到问题,您可以检查新字体的设置位置以及它是否存在。

let font = UIFont.init(name:"YOUR_FONT_NAME", size: 16) -> is it nil or not ?

回答by SteveA

I solved this by doing a check against [NSNull null]. The code I used in my app:

我通过对 [NSNull null] 进行检查来解决这个问题。我在我的应用程序中使用的代码:

_lblBusinessName.text = _business.BusinessName != [NSNull null] ? _business.BusinessName : @"";

However XCode threw some warnings so to get rid of those I casted to an NSObject* type, like this:

然而,XCode 抛出了一些警告,以便摆脱我转换为 NSObject* 类型的那些警告,如下所示:

_lblBusinessName.text = **(NSObject*)**_business.BusinessName != [NSNull null] ? _business.BusinessName : @"";

回答by Rob

Detect whether or not the object is null instead of trying to infer from the class name.

检测对象是否为空,而不是尝试从类名推断。

// if you're key may not exist (NSDictionary will return nil... not sure what type
// you are using
if (![object objectForKey:@"display_name"]){
    // ...
}

// or if the value may actually be an NSNull object
if ([[object objectForKey:@"display_name"] == (id)[NSNull null]){
    // ...
}

I haven't tested the second argument, but look here for more about testing null.

我还没有测试第二个参数,但请在此处查看有关测试 null 的更多信息