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!
self.adjis non-nil?self.adjis nil. Perhaps you are trying to add an object before loading that controller's view and need to move array initialization earlier or switch to a class constructor onNSMutableArraywhich will tolerate a nil argument.