Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C - Directly call method from array of objects

Tags:

objective-c

I'm new to Objective C, and I'm trying to call a method on an array (NSArray) of objects directly like this:

[[myPeople objectAtIndex: 0] setName: @"Shane"];

But this doesn't seem to work, and returns a warning saying "Multiple methods named 'setName' found"

I can successfully perform the operation in this manner:

Person* person = [myPeople objectAtIndex: 0];
[person setName: @"Shane"];

Is my syntax simply incorrect in the first case, or should the second piece of code be used? Or is there a better way that I'm not aware of?

Thanks, any help is greatly appreciated

like image 390
shane Avatar asked Jul 20 '26 09:07

shane


1 Answers

You can do it to all objects in the array like this:

[myPeople makeObjectsPerformSelector:@selector(setName:) withObject:@"Shane"];

like image 144
Michael Frederick Avatar answered Jul 22 '26 01:07

Michael Frederick