Reputation: 622
I have an NSArray that has to be an array for portions of the project so nothing will change it. I need to add an object to the array. The method I used was to convert to an NSMutableArray, add the object, and then convert back to an NSArray. The method:
- (void)addAdj:(NSString *)obj{
NSMutableArray *ary = [self.adj mutableCopy];
[ary addObject:obj];
self.adj=[ary copy];
for(int i = 0; i<[self.adj count]; i++){
NSLog(@"%@, ", [self.adj objectAtIndex:i]);
}
}
The for loop and log statement are included to print the array but it does not print anything at all. I have seen similar questions but people always tell the OP to just use an NSMutable array from the start. I'd like to know why this bit of code does not work as is. In advance Thanks!
Upvotes: 0
Views: 548
Reputation: 84
May be you have already forgot to initialize self.adj in viewDidload. You try to add below code to viewDidload:
self.adj = [[NSArray alloc]init];
Or you can show code in your viewDidload. I will be more help.
Upvotes: 1