Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nutthick

macrumors newbie
Original poster
Apr 11, 2007
26
0
I have a small application running out of a single class. It's not ideal, but I'm still learning. In the header of my class I have a NSMutableArray defined, that I manipulate in the .m file.

I have connected the open menu of my nib to an IBAction, that brings up an openPanel, with the selector defined as another method in my class. In that method I use the following call;

Code:
[myArray removeAllObjects];

The strange thing is that I can have any number of objects in the array, but when I put in a break point on this line it says that there are 0 objects. This is before the line is run. After the line is executed it still say 0 as you would expect. However when the application continues elsewhere, all the objects are back in the array.

The only thing I can think of is that the compiler is treating this instance of myArray as something different to the rest of the class, hence my question about scope.

To test this out I tried setting a BOOL flag and calling the removeAllObjects elsewhere in the code, based on it, but the same thing happens with the flag. I set it in the selector method but it is different to the instance in the rest of the class.

Can anyone explain this behaviour to me? Is this because I'm not using a MVC architecture?
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Make sure you don't have multiple instances of that class being initialized. You could override init to check.
 

nutthick

macrumors newbie
Original poster
Apr 11, 2007
26
0
In my NIB I have a custom view that I have set to my class (don't have XCode in front of me and can't remember the correct term) in the attribute inspector. I also have a NSObject that is set to my class that the menu items connect to. Would that end up creating two instances of the class?
 

nutthick

macrumors newbie
Original poster
Apr 11, 2007
26
0
kainjow - You were right, I was creating two instances. I re-architected the app to be a proper MVC and that sorted out the multiple instance problem.

Thanks for your help, this has been driving me nuts.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.