0

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!

3
  • Are you certain that self.adj is non-nil? Commented Jun 24, 2014 at 0:36
  • @dasblinkenlight Yes. I initialize it in the viewDidLoad method. Commented Jun 24, 2014 at 0:39
  • 1
    Time to break out the debugger. Add a break point at the first line of that method and I suspect you will find that self.adj is 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 on NSMutableArray which will tolerate a nil argument. Commented Jun 24, 2014 at 0:48

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.