2

If I have an NSMutableArray where I added objects of different classes (e.g. NSString, NSMutableString, NSProcessInfo, NSURL, NSMutableDictionary etc.) Now I want to fast enumerate this array, so I tried:

for (id *element in mutableArray){
   NSLog (@"Class Name: %@", [element class]);
   //do something else
}

I am getting a warning in Xcode saying

warning: invalid receiver type "id*"

How can I avoid this warning?

1 Answer 1

11

The code is almost correct. When you use id, it's already implied to be a pointer, so you should write it as:

for (id element in mutableArray){
   NSLog (@"Class Name: %@", [element class]);
   //do something else
}
Sign up to request clarification or add additional context in comments.

3 Comments

and you'd also probably want [element className] and not [element class].
well class is also printing out the name correctly, however className makes more sense while reading the code ... thanks for the tip!
Actually, -className is not meant for this purpose. That method is meant for scripting integration in Cocoa. -[Class description] should provide the correct result, but if you want to be pedantic, NSStringFromClass([element class]) is more correct

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.