• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Strange NSArray problem
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Strange NSArray problem


  • Subject: Re: Strange NSArray problem
  • From: Iain Delaney <email@hidden>
  • Date: Mon, 20 Jun 2005 19:35:38 -0400

Yes, that was the problem. It's the first time I've had a class object in the autorelease pool, so I'm not used to the behaviour.
Thanks.
On Jun 20, 2005, at 7:19 PM, John Stiles wrote:


The array is not being retained. When you use "arrayWithObjects" the array will be automatically freed sometime after your function returns (this is because it's in the autorelease pool--look up NSAutoreleasePool for more details on this).
You can use [myColors retain] to keep it from being freed. Then it is your responsibility to release it later with [myColors release].



On Jun 20, 2005, at 4:06 PM, Iain Delaney wrote:


I'm at my wit's end about this one, so I would appreciate any help or suggestions anyone can give.
I need an array of NSStrings to keep track of the different colors I'm using in my program.
I've declared the array as:
NSArray *myColors;
in the header for my controller class.
In the (id)init method of my controller class, I initialize the array, like this:
myColors = [NSArray arrayWithObjects: @"Blue", @"Red", @"Yellow", @"Green", nil];
Then I check the size of the array,
NSLog(@"Number of Colors: %d",[myColors count]);
And when I run it I see: "Number of Colors: 4". So far so good.


However, when I actually go to use the array in a button handler, it's gone. The button handler is a regular -(IBAction)buttonClick: (id)sender method, and I've changed the code so that all it does is log the count of the array, just like before:

NSLog(@"Number of Colors: %d",[myColors count]);
This time, though, the Run Log window says "[NSWorkspace count]: selector not recognized".


Some further information: When I go in and look with the debugger, myColors shows up with "4 objects" in the init method.
In the buttonClick method, the debugger window shows "{(int)[$VAR count]} objects".


I've removed every reference to myColors, except the initialization line and the NSLog statements, and I'm not using NSWorkspace anywhere in my code, so I don't know what's going on.

Can anyone help me with this?

Thanks,
Iain
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40blizzard.com


This email sent to email@hidden





_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Strange NSArray problem (From: Iain Delaney <email@hidden>)
 >Re: Strange NSArray problem (From: John Stiles <email@hidden>)

  • Prev by Date: Cocoa Memory Leaks
  • Next by Date: Re: NSLog doesn't work with OCUnit
  • Previous by thread: Re: Strange NSArray problem
  • Next by thread: Cocoa Memory Leaks
  • Index(es):
    • Date
    • Thread