Re: Problems with [NSArray count]
Re: Problems with [NSArray count]
- Subject: Re: Problems with [NSArray count]
- From: Peter Browne <email@hidden>
- Date: Tue, 22 Apr 2008 22:45:31 +0100
On 22 Apr 2008, at 22:15, David Duncan wrote:
On Apr 22, 2008, at 2:03 PM, Peter Browne wrote:
I'm returning to Cocoa after quite a long break, and it seems that
I'm a little rusty...
All I want to do is find out the number of items in an NSArray and
store that as a variable, which I can then find the square root of.
The docs tell me that [NSArray count] returns an NSUInteger, but
exactly WHAT one of these is, or how I use it is baffling me.
An NSUInteger is just an integer that is 32-bits or 64-bits
depending on if your application is compiled for 32-bit or 64-bit.
a simple
int i = [myArray count];
crashes out, and I've tried various other types of variable to no
avail. Any thoughts? Am I missing something blindingly obvious here?
Are you getting an exception? perhaps myArray isn't really an
NSArray at the time that you are sending it the -count message?
I'm getting an "EXC_BAD_ACCESS" error.
I've since managed to solve the problem by inserting a
[myArray retain];
but I'm not entirely sure WHY this fixed it...
The process followed is:
1) the myController object acquires data from a text file, using
NSString's -stringWithContentsOfFile:encoding:error
2) myArray then grabs this data (a bunch of numbers) using NSArray's -
componentsSeparatedByString:
3) myController then passes the myArray object to myView (for later
graph drawing) using my - (void)displayData:(NSArray *)theArray method
4) myView then calls [theArray count]
5) EXC_BAD_ACCESS
inserting a [myArray retain] just before passing the data to the view
fixed the problem. Why should this be the case?
As far as I'm aware the object isn't being released at that point, so
what's the need for a -retain message?
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden