iphone - [NSNull isEqualToString :]

标签 iphone ios string dictionary null

我正在尝试将字符串设置为“如果 [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];
}

最佳答案

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

当您的值为 [NSNull null] 时,您必须使用类似以下内容来管理案例:

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

关于iphone - [NSNull isEqualToString :],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6376492/

相关文章:

java - 这个正则表达式替换了什么?

iphone - Xcode 4 Interface Builder 性能缓慢

ios - NSJSONSerialization 错误 : Why am I not able to catch the error in try catch block? swift 3.0

ios - 升级到 iOS9 后,我在 iPhone 6、6s、6p 和 6sp 上的应用程序被拉伸(stretch)

iOS 架构和数据预取

c - 将大数字作为字符串进行操作

c# - 我如何 "ASCII armor"我的 MemoryStream?

iphone - 如何更改 UIBarButtonItem 图像大小

iphone - 正在调用 -[NSRunLoop runUntilDate :] a good idea?

ios - 为什么 iPhone X 会忽略 UICollectionView 的 UIEdgeInsets?