Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > newbie array question

newbie array question
Thread Tools
Junior Member
Join Date: Nov 2000
Status: Offline
Reply With Quote
Mar 12, 2001, 03:46 PM
 
Ok, for a program that I am doing, which will be open sourced as soon as it works, I need to create a NSMutableArray where each object is a 2 object NSArray or a NSString and an NSButton.

Two ways that I have approached this are as follows:
Code:
NSArray *arr; keyMap = [NSMutableArray arrayWithCapacity:40]; arr = [NSArray arrayWithObjects:@"s",t0]; (t0 is an outlet to a button from IB) [keyMap addObject:arr];
in this case, the compiler produces an error at [keyMap addObject:arr];

The other way that I hae approached this is to put it all in one line:
Code:
[keyMap addObject:[NSArray arrayWithObjects:@"s",t0]];
the compiler allows this, but then the program crashes on this line.

if it helps, keyMap is in the interface file as:
Code:
NSMutableArray *keyMap;
thanks,
Max
     
ali
Forum Regular
Join Date: Sep 2000
Status: Offline
Reply With Quote
Mar 12, 2001, 04:02 PM
 
When you do arrayWithObjects:, be sure to terminate the list of objects with NULL:

arr = [NSArray arrayWithObjects:@"s",t0, NULL];

You need this because arrayWithObjects: takes a variable number of arguments, and needs NULL to know when to stop. Lack of NULL probably explains your crash.

As far as the warning with addObject:, hmm, not sure what that is. What is the exact error message you get?


Ali



[This message has been edited by ali (edited 03-12-2001).]
     
Junior Member
Join Date: Nov 2000
Status: Offline
Reply With Quote
Mar 12, 2001, 04:16 PM
 
thank you Ali, that worked, but is this common to all functions that take a variable number of args?

coming form java, thats new to me.

-Max
     
Senior User
Join Date: Nov 2000
Status: Offline
Reply With Quote
Mar 12, 2001, 04:48 PM
 
coming from java it was new to me too

I use nil instead of NULL though. It saves me from having to use the shift key and type an extra letter.

good luck!
     
ali
Forum Regular
Join Date: Sep 2000
Status: Offline
Reply With Quote
Mar 12, 2001, 04:51 PM
 
Originally posted by mxcantor:
thank you Ali, that worked, but is this common to all functions that take a variable number of args?
Not always.

I believe in general a C function does not know how many arguments it has, so it has to use some other means to determine how many arguments to pull off the stack. In the case of arrayWithObjects: or dictionaryWithObjects:andKeys:, NULL is used to signal the end of the list. In case of something like stringWithFormat: and printf(), the argument specifiers in the format string indicate how many arguments to look for. There might be other techniques.

Ali


     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:41 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2