 |
 |
newbie array question
|
 |
|
 |
|
Junior Member
Join Date: Nov 2000
Status:
Offline
|
|
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
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Sep 2000
Status:
Offline
|
|
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
|
|
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
|
|
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!
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Sep 2000
Status:
Offline
|
|
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
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|