替代语法将json数据objectForKey分配给类的属性

时间:2016-05-24 09:46:11

标签: objective-c

我可以在一行中写下最后三行代码:

NSArray* latestLoans = [self.JsonData objectForKey:@"loans"];
for (id object in latestLoans) {
NSDictionary* loan = object;
newModelClass.name = [loan objectForKey:@"name"];
newModelClass.sector = [loan objectForKey:@"sector"];
newModelClass.activity = [loan objectForKey:@"activity"];

我的完整代码在这里,如果还有其他最佳做法,请建议我。

-(void)CopyOnlineData:(NSDictionary*)OnlineData{
 self.JsonData = OnlineData;
 NSArray* latestLoans = [self.JsonData objectForKey:@"loans"];
 for (id object in latestLoans) {
 NewModelClass *newModelClass = [[NewModelClass alloc] init];
 NSDictionary* loan = object;
 newModelClass.name = [loan objectForKey:@"name"];
 newModelClass.sector = [loan objectForKey:@"sector"];
 newModelClass.activity = [loan objectForKey:@"activity"];
 NSDictionary *loactionDictionary = loan[@"location"];
 newModelClass.country = loactionDictionary[@"country_code"];
 newModelClass.town = loactionDictionary[@"town"];
 NSDictionary *imageid = loan[@"image"];
 newModelClass.ImageId = imageid[@"id"];
 NSLog(@"name: %@ \n town: %@\n sector: %@\n country: %@\n activity: %@\n image id: %@", newModelClass.name,newModelClass.town,newModelClass.sector,newModelClass.country,newModelClass.activity,newModelClass.ImageId);
[self.tableData addObject:newModelClass];
}
[[self KivaTableView]reloadData];
}

谢谢,

1 个答案:

答案 0 :(得分:0)

继续我要做的事情:为NewModelClass创建一个自定义初始化,以便从这部分代码中删除它。这样,如果你必须创建一个NewModelClass的新对象,你就不必重写所有的行。

NewModelClass.h

-(id)initWithJSONDict:(NSDictionary *)dict;

NewModelClass.m

-(id)initWithJSONDict:(NSDictionary *)dict
{
    self = [super init];
    if (self)
    {
        self.name = [dict objectForKey:@"name"];
        self.sector = [dict objectForKey:@"sector"];
        self.activity = [dict objectForKey:@"activity"];
        NSDictionary *loactionDictionary = dict[@"location"];
        self.country = loactionDictionary[@"country_code"]; //or dict[@"location][@"country_code"];
        self.town = loactionDictionary[@"town"];//or dict[@"location][@"town"];
        NSDictionary *imageid = dict[@"image"];
        self.ImageId = imageid[@"id"];//or dict[@"image][@"id"];
    }
    return self;
}

我也会覆盖description

-(NSString *)description
{
   return [NSString stringWithFormat:@"<%@ %p>: name: %@ \n town: %@\n sector: %@\n country: %@\n activity: %@\n image id:", [self class], self, self.name, self.town, self.sector, self.country, self.activity, self.ImageId ];
}

然后在你的代码中:

-(void)addOnlineData:(NSDictionary*)onlineData
{
     self.JsonData = onlineData;
     NSArray* latestLoans = [self.JsonData objectForKey:@"loans"];
     for (NSDictionary *aLoan in latestLoans) 
     {
         NewModelClass *newModelClass = [[NewModelClass alloc] initWithJSONDict:aLoan];
         NSLog(@"Loan: %@", loan);
        [self.tableData addObject:newModelClass];
    }
    [[self KivaTableView]reloadData];
}

修改for循环,因为您已经知道它是NSDictionary,所以不使用for id,然后id强制转换为NSDictionary

方法名称的修改:
用小写字母启动方法。
避免以&#34; copy&#34;开头命名。如果你不做副本。

注意:此代码未经过测试,可能因语法错误而无法编译,但您应该了解主要想法。