Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

iphonedevelop18

macrumors member
Original poster
Feb 26, 2009
50
0
I've tried to search through these and other forums but i cannot figure out how to create an array.
 
I mean put objects into the array to be accessed later, like numbers or strings.
You've looked through the NSArray Class Reference, right? Is there something in particular you don't understand? Rather than give you the outright answer (which would be very easy), we're trying to guide you to educate yourself, a very handy skill to have!
 
After looking a bit more I found this piece of code.

Code:
 NSArray *array = [NSArray arrayWithObjects: @"John", @"Bob", [NSNull null], @"Jane"];

Now my main question is where do I declare this and also how do I put an NSNumber into an array like this one which just uses strings?
 
Where do you need to access this array?

in the .m viewController

The NSNull usage in that declaration should give you a hint.

So like this?

Code:
NSNumber *N1 = [numberWithInt:1]; 
NSNumber *N2 = [numberWithInt:2];
NSArray *Myarray = [NSArray initWithObjects:N1,N2,nil];
 
in the .m viewController
In multiple methods?

So like this?

Code:
NSNumber *N1 = [numberWithInt:1]; 
NSNumber *N2 = [numberWithInt:2];
NSArray *Myarray = [NSArray initWithObjects:N1,N2,nil];
Close. Your previous example used arrayWithObjects: Do you understand why? You can use initWithObjects: if you like, but there will be something else you'll need to do to your array first. Do you know what it is?
 
In multiple methods?

No it's just going to be used in one view controller

Close. Your previous example used arrayWithObjects: Do you understand why? You can use initWithObjects: if you like, but there will be something else you'll need to do to your array first. Do you know what it is?

Do I need to allocate it?
 
No it's just going to be used in one view controller
Yes, but a single view controller can contains multiple methods. If you only need access to it from within one method, declare and init your array in that method. If you need access to it from multiple methods, either declare it as a static or an instance variable, accordingly.

Do I need to allocate it?
Ding Ding Ding! We have a winner! Good job!
 
Thank you so much for helping me through this one rather than telling me the answer, really helped alot!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.