apolo
apolo

Reputation: 461

For loop when I don't know what objects are in an array?

So I have an array with some objects (NSString, NSNumber, etc.) and I want to loop through it using a for loop. I thought I'd use the id for the type.

NSArray *myArray = [[NSArray alloc] initWithObjects:@"one string",@"another", @3, nil];
for (id *something in myArray) {
....
}

What's wrong with the for loop above? Why can't I use id and what would be the appropriate "type" to use.

I am a begginer in iOS dev.

Upvotes: 0

Views: 62

Answers (2)

Linuxios
Linuxios

Reputation: 35788

id as a type already is a pointer, so id * is a pointer to a pointer, which is incorrect here. Try this:

for (id something in myArray) {

}

Upvotes: 2

David Berry
David Berry

Reputation: 41246

id is intrinsically a pointer, hence all you need is:

for(id something in myArray) {

btw, using the constant object syntax makes such code more legible:

@[ @"one string", @"another", @3 ]

Upvotes: 4

Related Questions