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?

Quinn Taylor
  • 44,553
  • 16
  • 113
  • 131
Dev
  • 6,207
  • 4
  • 27
  • 32

1 Answers1

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
}
pgb
  • 24,813
  • 12
  • 83
  • 113
  • and you'd also probably want `[element className]` and not `[element class]`. – Dave DeLong Aug 12 '09 at 18:54
  • well class is also printing out the name correctly, however className makes more sense while reading the code ... thanks for the tip! – Dev Aug 13 '09 at 03:02
  • 1
    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 – kperryua Aug 13 '09 at 04:06