Chr*_*ris 77 json objective-c ios
我有一个来自Web服务器的JSON对象.
日志是这样的:
{
"status":"success",
"UserID":15,
"Name":"John",
"DisplayName":"John",
"Surname":"Smith",
"Email":"email",
"Telephone":null,
"FullAccount":"true"
}
Run Code Online (Sandbox Code Playgroud)
请注意,如果用户未输入电话,则电话将为空.
将此值分配给a时NSString,将NSLog其显示为<null>
我正在分配这样的字符串:
NSString *tel = [jsonDictionary valueForKey:@"Telephone"];
Run Code Online (Sandbox Code Playgroud)
检查此<null>值的正确方法是什么?它阻止我保存NSDictionary.
我一直在使用的条件尝试[myString length]和myString == nil并myString == NULL
此外,iOS文档中最好的位置在哪里阅读?
Wev*_*vah 188
<null>NSNull单例是如何记录的.所以:
if (tel == (id)[NSNull null]) {
// tel is null
}
Run Code Online (Sandbox Code Playgroud)
(单例存在是因为您无法添加nil到集合类.)
Fle*_*lea 24
以下是演员表的示例:
if (tel == (NSString *)[NSNull null])
{
// do logic here
}
Run Code Online (Sandbox Code Playgroud)
Nit*_*hel 10
你也可以像这样检查这个Incoming String: -
if(tel==(id) [NSNull null] || [tel length]==0 || [tel isEqualToString:@""])
{
NSlog(@"Print check log");
}
else
{
NSlog(@Printcheck log %@",tel);
}
Run Code Online (Sandbox Code Playgroud)
如果您正在处理"不稳定"的API,您可能需要遍历所有键以检查null.我创建了一个类别来处理这个问题:
@interface NSDictionary (Safe)
-(NSDictionary *)removeNullValues;
@end
@implementation NSDictionary (Safe)
-(NSDictionary *)removeNullValues
{
NSMutableDictionary *mutDictionary = [self mutableCopy];
NSMutableArray *keysToDelete = [NSMutableArray array];
[mutDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if (obj == [NSNull null])
{
[keysToDelete addObject:key];
}
}];
[mutDictinary removeObjectsForKeys:keysToDelete];
return [mutDictinary copy];
}
@end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
67878 次 |
| 最近记录: |