PDA

View Full Version : How Do I create an array?




iphonedevelop18
Apr 6, 2009, 01:25 PM
I've tried to search through these and other forums but i cannot figure out how to create an array.



mccannmarc
Apr 6, 2009, 02:42 PM
I've tried to search through these and other forums but i cannot figure out how to create an array.

[]

themoonisdown09
Apr 6, 2009, 02:43 PM
You can create an array of objects by using NSArray or NSMutableArray.

iphonedevelop18
Apr 6, 2009, 06:57 PM
i know that but how do i set it up?

dejo
Apr 6, 2009, 07:52 PM
i know that but how do i set it up?
What do you mean "set it up"? Define it? Populate it? What?

iphonedevelop18
Apr 6, 2009, 08:17 PM
I mean put objects into the array to be accessed later, like numbers or strings.

eddietr
Apr 6, 2009, 09:59 PM
I mean put objects into the array to be accessed later, like numbers or strings.

In the documentation for NSArray, look under "Tasks" -> "Creating an array"

dejo
Apr 6, 2009, 10:27 PM
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!

iphonedevelop18
Apr 8, 2009, 09:32 AM
After looking a bit more I found this piece of 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?

dejo
Apr 8, 2009, 09:35 AM
Now my main question is where do I declare this...
Where do you need to access this array?

...and also how do I put an NSNumber into an array like this one which just uses strings?
The NSNull usage in that declaration should give you a hint.

iphonedevelop18
Apr 8, 2009, 09:45 AM
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?


NSNumber *N1 = [numberWithInt:1];
NSNumber *N2 = [numberWithInt:2];
NSArray *Myarray = [NSArray initWithObjects:N1,N2,nil];

dejo
Apr 8, 2009, 09:49 AM
in the .m viewController
In multiple methods?

So like this?


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?

iphonedevelop18
Apr 8, 2009, 09:53 AM
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?

dejo
Apr 8, 2009, 10:00 AM
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!

iphonedevelop18
Apr 8, 2009, 10:04 AM
Thank you so much for helping me through this one rather than telling me the answer, really helped alot!

dejo
Apr 8, 2009, 10:07 AM
Thank you so much for helping me through this one rather than telling me the answer, really helped alot!
You're welcome. Appreciate the feedback.