Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize Array of Objects using NSArray

I'm pretty new to Objective-C and iOS so I've been playing around with the Picker View. I've defined a Person Class so that when you create a new Person it automatically gives that person a name and age.

#import "Person.h"  @implementation Person  @synthesize personName, age;  -(id)init {     self = [super init];     if(self)     {         personName = [self randomName];         age = [self randomAge];     }      return self; }  -(NSString *) randomName {     NSString* name;     NSArray* nameArr = [NSArray arrayWithObjects: @"Jill Valentine", @"Peter Griffin", @"Meg Griffin", @"Jack Lolwut",                             @"Mike Roflcoptor", @"Cindy Woods", @"Jessica Windmill", @"Alexander The Great",                             @"Sarah Peterson", @"Scott Scottland", @"Geoff Fanta", @"Amanda Pope", @"Michael Meyers",                             @"Richard Biggus", @"Montey Python", @"Mike Wut", @"Fake Person", @"Chair",                             nil];     NSUInteger randomIndex = arc4random() % [nameArr count];     name = [nameArr objectAtIndex: randomIndex];     return name; }  -(NSInteger *) randomAge {     //lowerBound + arc4random() % (upperBound - lowerBound);     NSInteger* num = (NSInteger*)(1 + arc4random() % (99 - 1));      return num; }  @end 

Now I want to make an array of Persons so I can throw a bunch into the picker, pick one Person and show their age. First though I need to make an array of Persons. How do I make an array of objects, initialize and allocate them?

like image 814
Howdy_McGee Avatar asked Feb 20 '13 00:02

Howdy_McGee


People also ask

What is NSArray Objective-C?

An object representing a static ordered collection, for use instead of an Array constant in cases that require reference semantics.

What is difference between array and NSArray?

Array is a struct, therefore it is a value type in Swift. NSArray is an immutable Objective C class, therefore it is a reference type in Swift and it is bridged to Array<AnyObject> . NSMutableArray is the mutable subclass of NSArray . Because foo changes the local value of a and bar changes the reference.

How do you create an array of objects in Objective-C?

Creating an Array ObjectThe NSArray class contains a class method named arrayWithObjects that can be called upon to create a new array object and initialize it with elements. For example: NSArray *myColors; myColors = [NSArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];

How do I add elements to NSArray?

If you create an NSArray you won't be able to add elements to it, since it's immutable. You should try using NSMutableArray instead. Also, you inverted the order of alloc and init . alloc creates an instance and init initializes it.


2 Answers

There is also a shorthand of doing this:

NSArray *persons = @[person1, person2, person3]; 

It's equivalent to

NSArray *persons = [NSArray arrayWithObjects:person1, person2, person3, nil]; 

As iiFreeman said, you still need to do proper memory management if you're not using ARC.

like image 51
Allen Zeng Avatar answered Oct 10 '22 18:10

Allen Zeng


NSMutableArray *persons = [NSMutableArray array]; for (int i = 0; i < myPersonsCount; i++) {    [persons addObject:[[Person alloc] init]]; } NSArray *arrayOfPersons = [NSArray arrayWithArray:persons]; // if you want immutable array 

also you can reach this without using NSMutableArray:

NSArray *persons = [NSArray array]; for (int i = 0; i < myPersonsCount; i++) {    persons = [persons arrayByAddingObject:[[Person alloc] init]]; } 

One more thing - it's valid for ARC enabled environment, if you going to use it without ARC don't forget to add autoreleased objects into array!

[persons addObject:[[[Person alloc] init] autorelease]; 
like image 27
iiFreeman Avatar answered Oct 10 '22 19:10

iiFreeman