我有以下代码,无法获取保存的项目:
-(IBAction)saveButtonClicked:(UIButton *)sender
{
[myViewModel.itemsList addObject:myViewModel.items];
NSLog(@"Length of array .. %d", myViewModel.itemsList.count);
}
上面,我保存了我的对象数组,下面尝试获取相同的对象数组。 但是,对象数组(itemsList的items对象有属性名和desc。当我尝试获取myArray.name时,它说没有这样的属性。
-(IBAction)submitButtonClicked:(UIButton *)sender
{
UIAlertView *alert= [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Successful!"] message:@"Submitted successfuly!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
for(int i = 0; i< myViewModel.itemsList.count; i++){
NSLog(@"Array..%@", [myViewModel.itemsList objectAtIndex:i]);
NSObject *myArray = [myViewModel.itemsList objectAtIndex:i];
}
}
答案 0 :(得分:0)
而不是:
NSObject *myArray = [myViewModel.itemsList objectAtIndex:i];
执行:
WhateverYourCustomObjectClassIsCalled *myArray = [myViewModel.itemsList objectAtIndex:i];
答案 1 :(得分:0)
这几乎可以肯定是从数组中获取对象时你使用了错误的类型:
NSObject *myArray = [myViewModel.itemsList objectAtIndex:i];
应该是:
MyCustomObject *item = [myViewModel.itemsList objectAtIndex:i];
还可以使用快速枚举,错误,更快捷,更方便:
for (MyCustomObject *item in myViewModel.itemsList) {
NSLog(@"name=%@", item.name);
}
顺便说一句,这个 会起作用:
for (id item in myViewModel.itemsList) {
NSLog(@"name=%@", [item name]);
}