I am new coding in Objective-C and I am trying to figure out how to do the following:
I have a class Company:
@interface Company : NSObject
@property (strong, nonatomic)NSString *companyID;
@property (strong, nonatomic)NSString *companyName;
@end
I am getting a dictionary from a server and just parsing in objects. After that, I am adding these objects in an array (It´s working - no problem here - I got the JSON).
NSMutableDictionary *companyDictionary = [[NSMutableDictionary alloc]initWithDictionary:response];
NSArray *companyArray = [companyDictionary valueForKey:@"data"];
NSMutableArray *companyInformationArray = [NSMutableArray new];
//Parser
for (int i = 0; i < [companyArray count]; i++) {
Company *company = [Company new];
company.companyID = [[companyArray objectAtIndex:i] valueForKey:@"company_id"];
company.companyName = [[companyArray objectAtIndex:i] valueForKey:@"name"];
[companyInformationArray addObject:company];
}
THE PROBLEM IS: I need to access the objects and its fields inside
companyInformationArray
I am just trying to do something similar with (these two approaching will not work of course):
Company *company = [Company new];
company = [[companyInformationArray objectAtIndex:0] valueForKey:@"name"];
Or:
Company *company = [Company new];
company.companyName = [[companyInformationArray objectAtIndex:0] companyName];
Could you please help me?
Thanks!
copyrather thanstrongfor yourNSStringproperties of classes likeCompany. By usingstrong, if the string happened to be aNSMutableStringand it was mutated later, you risk having yourCompanyobject unintentionally mutate, too. By making itcopy, you avoid this (admittedly edge-case) scenario.