Mario Mario - 1 year ago 31
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]; = @"Mary";
item1.Description = @"good girl";
ViewController *item2 = [ViewController new]; = @"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 Source

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 ([ isEqualToString:@"Mary"]) { //you missed the opening "[" and closing "]"
        _DescriptionLabel.text= item.Description;