Ayaka Nonaka
Ayaka Nonaka

Reputation: 856

Adding nil to NSMutableArray

I am trying to create a NSMutableArray by reading in a .txt file and am having trouble setting the last element of the array to nil.

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"namelist" ofType:@"txt"];
NSString *data = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSArray *list = [data componentsSeparatedByString:@"\n"];
NSMutableArray *mutableList = [[NSMutableArray alloc] initWithArray:list];

I wanted to use NSMutableArray's function addObject, but that will not allow me to add nil. I also tried:

[mutableList addObject:[NSNull null]];

but that does not seem to work either. Is there a way around this problem?

Upvotes: 1

Views: 2553

Answers (2)

jrtc27
jrtc27

Reputation: 8526

Use

NSMutableArray *mutableList = [[NSMutableArray alloc] init];
[mutableList addObjectsFromArray:list];

Hope this helps jrtc27

Upvotes: 0

Michael D. Irizarry
Michael D. Irizarry

Reputation: 6302

Per Apple's documentation on NSMutableArray.

addObject:

Inserts a given object at the end of the receiver.

- (void)addObject:(id)anObject
Parameters

anObject

    The object to add to the end of the receiver's content. **This value must not be nil.**

Reference

http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html

Upvotes: 0

Related Questions