我想将对象添加到nsdictionary并访问这些对象或修改对象中的属性,但是我无法获取它们。
例如:
ObjectClass:
-(id)init
{
self = [super init];
if (self)
{
self.firstName = @"";
self.lastName = @"";
}
return self;
}
mainClass:
ObjectClass *myObject = [ObjectClass allo] init];
NSMutableDictionary *listOfClients = [NSMutableDictionary allo] init];
[self.listOfClients setObject:myObject forKey:@"firstClient"];
(lldb) po [listOfClients valueForKey:@"firstClient"]
$5 = 0x0683e140 <myObject: 0x683e140>
(lldb) po [_testDic valueForKey:@"firstClient.myObject"]
$6 = 0x00000000 <nil>
如何访问firstClient的firstName?
我真的很感谢你的帮助
答案 0 :(得分:1)
首先,您的listOfClients
本地变量和您的财产存在命名冲突。尝试这样的事情:
self.listOfClients = [[NSMutableDictionary alloc] init];
ObjectClass *myObject = [[ObjectClass alloc] init];
myObject.firstName = @"John";
myObject.lastName = @"Smith";
self.listOfClients[@"firstClient"] = myObject;
这将完成初始化字典和对象并将对象放入字典中。然后你可以访问这样的项目:
ObjectClass *obj = (ObjectClass *)self.listOfClients[@"firstClient"];
NSLog(@"Name: %@ %@", obj.firstName, obj.lastName);
答案 1 :(得分:0)
我完全错过了这个:
[listOfClients valueForKeyPath:@“firstClient.lastName”];