initialize an NSArray with a list of objects
-
Is there a way to initialize an NSArray with a list of strings like I
can do in C
Example
the C way
char * myList[] = {"item1", "item2", "item3" }
I tried this, and it only picked up the first item
NSArray * myList = {@"item1", @"item2", @"item3" }
Thanks for the help
-dave -
> Is there a way to initialize an NSArray with a list of strings like
> I can do in C
>
> the C way
> char * myList[] = {"item1", "item2", "item3" }
The first thing that comes to mind is:
NSArray *myArray = [NSArray arrayWithObjects:@"item1", @"item2",
@"item3", nil];
It seems unlikely that there would be an operator (like {}) that
would create cocoa collection objects grafted into objc.
Cheers,
-Joshua Emmons -
On 5/16/06, David Alter <David...> wrote:
> Is there a way to initialize an NSArray with a list of strings like I
> can do in C
>
> Example
>
> the C way
> char * myList[] = {"item1", "item2", "item3" }
>
>
> I tried this, and it only picked up the first item
>
> NSArray * myList = {@"item1", @"item2", @"item3" }
>
> Thanks for the help
NSArray* myList = [NSArray arrayWithObjects:@"item1", @"item2", @"item3", nil];
(note you will need to retain myList if you want to have the array
last past the current autorelease pool)
Anyway the docs for NSArray outline these and related API
(initWithObjects:, etc.) and all that is available directly from Xcode
via a simple option double click of a class name (i.e. option-double
click "NSArray").
<file:///Developer/ADC%20Reference%20Library/documentation/Cocoa/Reference/Foundation/ObjC_classic/Classes/NSArray.html#//apple_ref/occ/clm/NSArray/arrayWithObject:>
<file:///Developer/ADC%20Reference%20Library/documentation/Cocoa/Reference/Foundation/ObjC_classic/Classes/NSArray.html#//apple_ref/occ/instm/NSArray/initWithObjects:>
- Shawn -
On May 16, 2006, at 3:53 PM, David Alter wrote:
> Is there a way to initialize an NSArray with a list of strings like
> I can do in C
>
> Example
>
> the C way
> char * myList[] = {"item1", "item2", "item3" }
>
>
> I tried this, and it only picked up the first item
>
> NSArray * myList = {@"item1", @"item2", @"item3" }
>
> Thanks for the help
> -dave
You'll want to use the arrayWithObjects class method, and remember to
cap it off with nil. Use like this:
NSArray *myList = [NSArray arrayWithObjects: @"item1", @"item2",
@"item3", nil];
- Andrew Bowman -
Sort of. You can't use the curly brackets, but you can initialize it
with a variable-length list.
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/
ObjC_classic/Classes/NSArray.html#//apple_ref/occ/clm/NSArray/
arrayWithObjects:
On May 16, 2006, at 3:53 PM, David Alter wrote:
> Is there a way to initialize an NSArray with a list of strings like
> I can do in C
>
> Example
>
> the C way
> char * myList[] = {"item1", "item2", "item3" }
>
>
> I tried this, and it only picked up the first item
>
> NSArray * myList = {@"item1", @"item2", @"item3" }
>
> Thanks for the help
> -dave
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Cocoa-dev mailing list (<Cocoa-dev...>)
> Help/Unsubscribe/Update your Subscription:
> http://lists.apple.com/mailman/options/cocoa-dev/<enigma0...>
>
> This email sent to <enigma0...>