比较 NSString 和有关 NSNumber 的警告
有时,JSON 从获取数据的数据库返回 (null)
,因此我会检查它是否已返回该 (null)
。
else if (NSOrderedSame == [[clubsArray objectAtIndex:indexPath.row] compare:@"(null)"] )
但 Xcode 警告我
从不同的 Objective-C 类型传递“compare:”的参数 1 时,不兼容的 Objective-C 类型“struct NSString *”,预期为“struct NSNumber *”
我将 NSString
添加到该数组中,所以我很困惑为什么它谈论一个结构。
NSString *clubNameReturned = [message objectForKey:@"clubname"];
[clubsArray addObject:clubNameReturned];
有人能够阐明这里发生的事情吗?
代码按照我的预期执行,但我不想做一些不正确的事情。
Sometimes JSON returns (null)
from the database where it gets data, so I do a check to see if its has returned that (null)
.
else if (NSOrderedSame == [[clubsArray objectAtIndex:indexPath.row] compare:@"(null)"] )
But Xcode is warning me
Incompatible Objective-C types 'struct NSString *', expected 'struct NSNumber *' when passing argument 1 of 'compare:' from distinct Objective-C type
I add NSString
s into that array so I'm confused as to why its talking about a struct.
NSString *clubNameReturned = [message objectForKey:@"clubname"];
[clubsArray addObject:clubNameReturned];
Is anybody able to shed some light on what's going on here?
The code executes as I expect it to, but I don't want to be doing something that's not correct.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
JSON 不返回“(null)”字符串。这个字符串“(null)”只是当你 NSLog 空单例对象时控制台上显示的内容(它是
description
表示形式)。与
[NSNull null]
进行比较,即 一个单例,特别用于存储表示容器上的 null/nil 值的内容。JSON does not return the "(null)" string. This string "(null)" is only what is displayed on the console when you NSLog the null singleton object (it's
description
representation).Compare against
[NSNull null]
instead, which is a singleton especially used to store something that represent the null/nil value on containers.这是因为
compare:
方法的定义是:因此它需要一个 NSNumber 对象,但您传递的是一个 NSString。
如果您想检查 2 个 NSString 是否相同,请使用 isEqualToString: 方法:
但是,它可能不是返回的字符串(尽管这是您在打印到控制台时看到的内容),因此您可以应该做:
That is because the definition of the
compare:
method is:So it is expecting an NSNumber object, but you are passing an NSString.
If you want to check if 2 NSStrings are the same use the
isEqualToString:
method:However it is likely not a string being returned (although that's what you see when it's printed to the console), so instead you should do:
如果您显式向下转换,则警告将消失,例如:
if (NSOrderedSame == [(NSString *)[clubsArray objectAtIndex:indexPath.row]compare:@"(null)"] )
发生的情况是您正在对
[clubsArray objectAtIndex:indexPath.row]
(类型为id
)的结果调用compare:
方法。现在,允许从 id 隐式向下转换为任何其他类型...但在您的情况下,编译器无法告诉您想要的确切类型。是NSNumber
或NSString
还是NSDate
...? (顺便说一句,NSObject
不响应compare:
)。这是一个警告,因为编译器认为您将在运行时提供正确的类型(您确实这样做了!),但它告诉您应该更明确地使用此类代码。The warning will go away if you explicitly downcast, e.g.:
if (NSOrderedSame == [(NSString *)[clubsArray objectAtIndex:indexPath.row] compare:@"(null)"] )
What happens is that you're calling the
compare:
method on the result of[clubsArray objectAtIndex:indexPath.row]
, which is of typeid
. Now, implicit downcasting is allowed fromid
to any other type... but in your case, the compiler cannot tell the exact type you want. Is itNSNumber
orNSString
orNSDate
...? (BTW,NSObject
does not respond tocompare:
). This is a warning because the compiler thinks you will provide the correct types during runtime (which you do!), but it tells you that you should be more explicit with that kind of code.我建议您在 NSDictionary 上使用此类别来处理 JSON 数据中的空值:
TouchJSON ,处理NSNull
I suggest you make use of this category on NSDictionary for dealing with null values in JSON data:
TouchJSON, dealing with NSNull