Mario Mario - 4 months ago 8
Objective-C Question

Objective-C. access to property of item in array which consists of objects

I have the array which consists of objects:

ViewController *item1 = [ViewController new];
item1.name = @"Mary";
item1.Description = @"good girl";
ViewController *item2 = [ViewController new];
item2.name = @"Daniel";
item2.Description = @"bad boy";
ComplexArray= [NSArray arrayWithObjects: item1, item2, nil];`


i want to view in labels a name and description if name is equal Mary

for (int i = 0; i < [ComplexArray count]; i++) {
if (item[i].name isEqualString:@"Mary") {
_nameLabel.text= item[i].name;
_DescriptionLabel.text= item[i].Description;
}
}


Please help me

Answer

Your problem is you didn't assign anything in item variable. Just update like this and it will work.

for (int i = 0; i < [ComplexArray count]; i++) {
    ViewController *item = [ComplexArray objectAtIndex:i]; // you missed this line.
    if ([item.name isEqualToString:@"Mary"]) { //you missed the opening "[" and closing "]"
        _nameLabel.text= item.name;
        _DescriptionLabel.text= item.Description;
    }
}
Comments