Nigel
Nigel

Reputation: 604

Objective C /iPhone : Is it possible to re initialize an NSArray?

I read that non mutable data types can't be modified once created.(eg NSString or NSArray).

But can they be re-initialized to point to a different set of objects?

If so, do I use release to free any alloc from first time round in between uses? eg:

myArray declared as NSArray *myArray in interface, and as nonatomic/retain property.myArray set in initialization code to a point to an array of strings as follows.

self.myArray = [myString componentsSeparatedByString:@","];

But later I want to re-initialize myArray to point to a different set of strings

self.myArray = [myOtherString componentsSeparatedByString:@","];

Is it possible? Thanks...

Upvotes: 1

Views: 1325

Answers (3)

Jim
Jim

Reputation: 4719

Yes you can reinitialized the NSArray. Here is the sample code that i used to re-initialized the NSArray.

NSString *keywords = @"FirstName|LastName|Address|PhoneNumber";
NSArray *arr = [keywords componentsSeparatedByString:@"|"];
NSLog(@"First Init - %@,%@,%@,%@",[arr objectAtIndex:0],[arr objectAtIndex:1],
                                  [arr objectAtIndex:2],[arr objectAtIndex:3]);
arr = nil;

keywords = @"First_Name|Last_Name|_Address|_PhoneNumber";
arr = [keywords componentsSeparatedByString:@"|"];
NSLog(@"Second Init - %@,%@,%@,%@",[arr objectAtIndex:0],[arr objectAtIndex:1],
                                   [arr objectAtIndex:2],[arr objectAtIndex:3]);

Upvotes: 0

Georg Schölly
Georg Schölly

Reputation: 126105

It really depends what you mean with re-initialize. You can assign another immutable object to a pointer, because the pointers aren't constant.

Example:

@interface MyObj : NSObject {
    NSString *name;                    // not needed in 64bit runtime AFAIK
}
@property(retain) NSString *name;      // sane people use copy instead of retain
                                       // whenever possible. Using retain can
                                       // lead to some hard to find errors.
@end

/* ... another file ... */

MyObj *theObject = [[[MyObj alloc] init] autorelease];

theObject.name = @"Peter";
NSString *oldName = theObject.name;

NSLog(@"%@", theObject.name);   // -> Peter
NSLog(@"%@", oldName);          // -> Peter

theObject.name = @"Martin";
NSLog(@"%@", theObject.name)    // -> Martin
NSLog(@"%@", oldName)           // -> Peter

If the behavior above is what you want, that's fine.

If you want that last line to return Martin you're in trouble. Those are constant strings and are not meant to be modified. You could, if you really want, modify the memory of the object directly, but this is dangerous and not recommended. Use mutable objects if you need such behaviour.

Upvotes: 2

Jack
Jack

Reputation: 133577

Of course they can. Saying that an NSArray is immutable doesn't mean that an attribute of a class of that type cannot be changed. You can't change the content, but you can assign new content to it.

If you want to make also changing the reference impossible you should use const keyword.

Upvotes: 0

Related Questions