将 NSNull 对象分配给 NSString [英] Assign NSNull Object to NSString
本文介绍了将 NSNull 对象分配给 NSString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一个 iOS 应用程序,我需要通过 Azure 的移动服务从 SQL 数据库中获取数据.
I'm writing an iOS App where i need to get data from a SQL-Database over mobile Services from Azure.
下载数据后,我得到一个 NSDictionary
,其中包含 SQL 表中的所有属性.如果属性为空,则值为 NSNull
.
After downloading the data I get a NSDictionary
with all attributes from the SQL-Table. If an attribute is empty, the value is NSNull
.
有没有办法在没有 IF 语句的情况下将 NSNull
传递给 NSString
(我不想有 20 个 if 语句......)?
Is there a way to pass NSNull
to NSString
without an IF-Statement (I don't want to have 20 if statements..)?
推荐答案
我写了一个分类就是为了解决这个问题.我将它与 Core Data 一起使用,但它也应该对您有所帮助.
I wrote a category just for dealing with this issue. I used it with Core Data but it should help you, too.
@interface NSDictionary (Extensions)
- (id)NSNullToNilForKey:(NSString *)key;
@end
@implementation NSDictionary (Extensions)
- (id)NSNullToNilForKey:(NSString *)key
{
id value = [self valueForKey:key];
return value != [NSNull null] ? value : nil;
}
@end
示例使用:
NSString *value = [dictionary NSNullToNilForKey:@"key"];
这篇关于将 NSNull 对象分配给 NSString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文