0

I've already looked at the other SO discussions but can't seem to find an answer. I'm getting this answer after basically the following code:

1) In the class that owns the AwesomeMenu, I am making an NSMutableArray of AwesomeMenuItems, which is required to initialize the AwesomeMenu:

NSMutableArray *menus = [[[NSMutableArray alloc] init] autorelease];
[menus addObject:starMenuItem1];
[menus addObject:starMenuItem2];
[menus addObject:starMenuItem3];
[menus addObject:starMenuItem4];
[menus addObject:starMenuItem5];

AwesomeMenu *menu = [[[AwesomeMenu alloc] initWithFrame:self.view.bounds menus:menus view:self.view] autorelease];

2) In AwesomeMenu.h, I have the array declared in the interface like so:

NSMutableArray *_menusArray;

And I've made a property for the array:

@property (nonatomic, retain) NSMutableArray *menusArray;

and it is synthesized:

@synthesize menusArray = _menusArray;

3) In the initWithFrame: menus: view: , I assign the menus parameter like so:

- (id)initWithFrame:(CGRect)frame menus:(NSMutableArray *)aMenusArray view:(UIView*)gView
{
    self = [super initWithFrame:frame];
    // ... some code
    self.menusArray = aMenusArray; // BREAKPOINT HERE.
    // ... some more code
}

4) I put a breakpoint at the assignment and here is what happens. Immediately before that line is run, I check my self.menusArray and it is: (NSMutableArray *)0x00000000 0 Objects

I step over the line and it becomes: (__NSArrayI *) 0x06045070 5 objects.

While this is the correct number of objects, it is no longer a NSMutableArray which runs me into problems later!

What is happening and how can I stop it from happening?

2 Answers 2

0

Check the declaration of the following in your AwesomeMenu.h file. Make sure that you change the the NSArray to NSMutableArray there as well. That also could cause this issue.

- (id)initWithFrame:(CGRect)frame menus:(NSMutableArray *)aMenusArray view:(UIView*)gView;

Update: Just a note. In init method always use the following after allocating memory for self.

if (self) {
   //code goes here
}

Update2: Also change self.menusArray = aMenusArray; to _menusArray = [aMenusArray retain]

As per apple documentation you are not supposed to use self.param in init and dealloc method since it is in the process of memory allocation/destruction which is not complete yet.

Sign up to request clarification or add additional context in comments.

12 Comments

It is declared as a mutable array in the header file. No problems there. Thanks for the tip, adding it.
What about the this line in your .h file - (id)initWithFrame:(CGRect)frame menus:(NSMutableArray )aMenusArray view:(UIView)gView; Does it have NSMutableArray or NSArray in it? Can you please post that line from your .h file.
- (id)initWithFrame:(CGRect)frame menus:(NSMutableArray )aMenusArray view:(UIView) gView; This looks right, right? This is the line from the .h file, directly pasted.
The asterisks are removed in the comments. The NSMutableArray and the UIView are pointers.
That looks fine for me. So are you using ARC? If not can you change your strong keyword to retain and see what happens? Other than that I dont see anything wrong with your code here.
|
0

__NSArrayI it's inmutable array, you are passing to initWithFrame method NSArray, not NSMutable array

Solution:

self.menusArray = [NSMutableArray arrayWithArray:aMenusArray]; // BREAKPOINT HERE.

7 Comments

I checked the aMenusArray I'm passing in. It is a Mutable Array prior to and after assignment. It is only the self.menusArray that somehow turns into the __NSArrayI type. It makes no sense to me.
Also, to confirm, I tried the above and it did not work for me.
I would propose removeObjectAtIndex, however I cannot because I'm stuck with a __NSArrayI type instead of a NSMutableArray. Do you understand that I am dealing with an immutable array when I expect it to be mutable?
i understand, but i have a lack of code and can't fully figure out. I asked you for more code to understand it. So, code this remove items will help
It's not my function - it belongs to NSMutableArray. developer.apple.com/library/mac/#documentation/Cocoa/Reference/…
|

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.