0

Now I'm working on an iPhone project and I'm using instances of the class NSMutableArray and suddenly, with no reason, at execution time the NSMutableArray converts into a UICachedDeviceRGBColor becoming unusable.

I do not know what to do, the same object had been working fine until today.

I got this error:

*** -[UICachedDeviceRGBColor count]: unrecognized selector sent to instance 0x4b3e440
Alex Rozanski
  • 37,815
  • 10
  • 68
  • 69
  • Sounds like a memory allocation issue. You should show some code. It's not possible for it to automatically "convert" into some other object. What does your code to initialize the NSMutableArray look like? – Matt Long Oct 01 '09 at 17:35
  • hi Matt!! let me add you the code: NSMutableArray *currentIngsGroup = [[NSMutableArray alloc] init]; currentIngsGroup = (NSMutableArray *)[allIngredientGroups objectAtIndex:0]; NSLog(@"accesing array %d", [currentIngsGroup count]); // here it crashes because detects the currentIngsGroup as UICachedDeviceRGBColor object :( – Alejandra Gonzalez Oct 01 '09 at 17:56
  • If you put your code up in your question, you can take advantage of the auto-formatting and make it a lot more readable. – willc2 Oct 05 '09 at 06:34
  • And then I scroll down 1 inch and see bbum's comment. Oops. – willc2 Oct 05 '09 at 06:35

1 Answers1

4

Next time, stick your code in your question, not the comment. Here it is nicely formatted:

NSMutableArray *currentIngsGroup = [[NSMutableArray alloc] init];
currentIngsGroup = (NSMutableArray *)[allIngredientGroups objectAtIndex:0];
NSLog(@"accesing array %d", [currentIngsGroup count]); 

Normally, I would say that the symptom you describe indicates that you are over-releasing something. The code, though, demonstrates a fundamental lack of understanding of Objective-C. I would suggest reading the Introduction to Objective-C document.

In particular, you are allocating an instance of NSMutableArray in the first line of code. The second line, though, immediately overwrites the mutable array reference with a reference to whatever object is at index 0 of the allIngredientGroups array.

The (NSMutableArray *) is totally unnecessary. It won't force whatever object is in the array to be a mutable array and, since -objectAtIndex: returns an (id) a cast isn't necessary.

BTW: The array allocated on the first line is being leaked. Again, read the introduction to objective-c to understand why.

bbum
  • 162,346
  • 23
  • 271
  • 359
  • Thanks bbum! Sorry because I didn't place my code in the correct place and I didn't explain that the code lines are was taken from the entire main controller (they are not together). Thanks a lot for your help. I couldn't make the app to work again, thanks God I had a working version at the SVN. Thanks again Alejandra :) – Alejandra Gonzalez Oct 01 '09 at 20:54
  • Happy to help. Seriously -- go read the intro guide. It is good stuff!! – bbum Oct 02 '09 at 04:56