gpt4 book ai didi

ios - 如何检测字典是否为空或 null

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:01:52 26 4
gpt4 key购买 nike

我收到一个 JSON 字符串,我需要对其进行迭代以检索一些对象值。这是结构

-meta-objects   |_cabdriver              |_employee   |client

There are objects under the objects tree and there are also child nodes, like cabdriver and client. The child node cabdriver has also another child node called employee.

This is the way I am iterating it:

NSArray *messageArray = [json objectForKey:@"objects"];
historialServicios = [[NSMutableArray alloc]init];

// Parse and loop through the JSON
for (dictionary in messageArray) {
//datos de nivel objects
NSString * date = [dictionary objectForKey:@"date"];
NSString * origin = [dictionary objectForKey:@"origin"];
NSString * destiny = [dictionary objectForKey:@"destiny"];
NSString * rate = [dictionary objectForKey:@"service_rate"];
NSString * state = [dictionary objectForKey:@"state"];
NSString * time_service = [dictionary objectForKey:@"time_service"];
NSString * id_service = [dictionary objectForKey:@"id"];

//datos de nivel cliente
NSDictionary *level2Dict = [dictionary objectForKey:@"client"];
NSString *client_id = [level2Dict objectForKey:@"id"];

//datos de nivel cabdriver
NSDictionary *cabdriverLevelDict=[dictionary objectForKey:@"cabdriver"];

//datos de nivel employee
NSDictionary *employeeLevelDict = [cabdriverLevelDict objectForKey:@"employee"];

//datos del employee
NSString *driverName = [employeeLevelDict objectForKey:@"name"];
NSString *driverLastname = [employeeLevelDict objectForKey:@"lastname"];
NSString *driverPhone = [employeeLevelDict objectForKey:@"phone"];
NSString *driverId = [employeeLevelDict objectForKey:@"id"];

[historialServicios addObject:@{
@"time_service": time_service,
@"id_service": id_service,
@"rate": rate,
@"destiny": destiny,
@"state": state,
@"origin": origin,
@"client_id":client_id,
@"date": date,
@"driverName":driverName,
@"driverLastname": driverLastname,
@"driverPhone": driverPhone,
@"driverId": driverId

}];
NSLog(@"DESPUES DE ANADIR OBJETOS");
NSLog(@"OBJETO ANADIDO==>TIME SERVICE = %@, ID SERVICE=%@, SERVICE RATE=%@,SERVICE DATE=%@,DESTINY=%@, STATE =%@,CLIENT ID=%@, ORIGIN=%@,DRIVER NAME=%@, DRIVER LASTNAME=%@,DRIVER PHONE=%@, DRIVER ID=%@",time_service,id_service,rate,date,destiny,state,client_id,origin,driverName,driverLastname,driverPhone,driverId);

//insertamos objetos en diccionario historialServicios
}

如果对象具有所有节点,一切正常,但有时,节点 cabdriver 为空且没有员工子节点。如果是这种情况,我会抛出异常并且应用程序崩溃。

如何判断节点员工不存在,避免获取异常?

谢谢。

最佳答案

您可以声明一个类别来处理注入(inject)到您的 json 中的 [NSNull null] 值。

@interface NSDictionary (NilNull)
- (id)optionalObjectForKey:(id)key;
- (id)optionalObjectForKey:(id)key defaultValue:(id)defaultValue;
@end

@implementation NSDictionary (NilNull)
- (id)optionalObjectForKey:(id)key {
return [self optionalObjectForKey:key defaultValue:nil];
]
- (id)optionalObjectForKey:(id)key defaultValue:(id)defaultValue {
id obj = [self objectForKey:key];
return (obj == [NSNull null] || !obj) ? defaultValue : obj;
}
@end

然后改用它:

NSDictionary *cabdriverLevelDict = [dictionary optionalObjectForKey:@"cabdriver"];
NSDictionary *employeeLevelDict = [cabdriverLevelDict optionalObjectForKey:@"employee"];

您还没有发布异常的内容,但从它的外观来看,它可能与尝试将 nil 值添加到您的新字典有关。

然后使用默认值 [NSNull null] 进行所有生成对象的数据查找,您将使用这些对象构建最终字典。完整的查找源现在将如下所示:

NSString * date = [dictionary optionalObjectForKey:@"date" defaultValue:[NSNull null]];
NSString * origin = [dictionary optionalObjectForKey:@"origin" defaultValue:[NSNull null]];
NSString * destiny = [dictionary optionalObjectForKey:@"destiny" defaultValue:[NSNull null]];
NSString * rate = [dictionary optionalObjectForKey:@"service_rate" defaultValue:[NSNull null]];
NSString * state = [dictionary optionalObjectForKey:@"state" defaultValue:[NSNull null]];
NSString * time_service = [dictionary optionalObjectForKey:@"time_service" defaultValue:[NSNull null]];
NSString * id_service = [dictionary optionalObjectForKey:@"id" defaultValue:[NSNull null]];

//datos de nivel cliente
NSDictionary *level2Dict = [dictionary optionalObjectForKey:@"client" defaultValue:[NSDictionary dictionary]];
NSString *client_id = [level2Dict optionalObjectForKey:@"id" defaultValue:[NSNull null]];

//datos de nivel cabdriver
NSDictionary *cabdriverLevelDict=[dictionary optionalObjectForKey:@"cabdriver" defaultValue:[NSDictionary dictionary]];

//datos de nivel employee
NSDictionary *employeeLevelDict = [cabdriverLevelDict optionalObjectForKey:@"employee" defaultValue:[NSDictionary dictionary]];

//datos del employee
NSString *driverName = [employeeLevelDict optionalObjectForKey:@"name" defaultValue:[NSNull null]];
NSString *driverLastname = [employeeLevelDict optionalObjectForKey:@"lastname" defaultValue:[NSNull null]];
NSString *driverPhone = [employeeLevelDict optionalObjectForKey:@"phone" defaultValue:[NSNull null]];
NSString *driverId = [employeeLevelDict optionalObjectForKey:@"id" defaultValue:[NSNull null]];

关于ios - 如何检测字典是否为空或 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29127460/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com