简体   繁体   中英

NSMutableArray addObject returns nil

There are some similar questions that already exist on StackOverflow. I did check them out, and in most cases it returns nil because the NSMutableArray has not been initialised. But in my case I did initialise it.

Here's part of my code :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.storedTimeZones = [[NSMutableArray alloc] init];
    NSData *storedData = [[NSUserDefaults standardUserDefaults] dataForKey:@"timeZones"];

    if (storedData != nil ) {
        self.storedTimeZones = [NSKeyedUnarchiver unarchiveObjectWithData:storedData];
    }

    NSString *str = [self.smallRegionArray[indexPath.row] stringByReplacingOccurrencesOfString:@"_" withString:@" "];
    [self.storedTimeZones addObject: str];
    NSLog(str); //the string was printed successfully.
    NSLog(@"%lu", (unsigned long)self.storedTimeZones.count); //'0' was printed
}

update @Caleb was right, [NSKeyedUnarchiver unarchiveObjectWithData:storedData returned nil. I solved it by doing this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.storedTimeZones = [[NSMutableArray alloc] init];
    NSData *storedData = [[NSUserDefaults standardUserDefaults] dataForKey:@"timeZones"];
    NSMutableArray *ary = [NSKeyedUnarchiver unarchiveObjectWithData:storedData];

    if (ary != nil ) {
        self.storedTimeZones = ary;
    }

    NSString *str = [self.smallRegionArray[indexPath.row] stringByReplacingOccurrencesOfString:@"_" withString:@" "];
    [self.storedTimeZones addObject: str];
    NSLog(str);
    NSLog(@"%lu", (unsigned long)self.storedTimeZones.count); //Now it prints `1`
}

But in my case I did initialise it.

You did, but then you replaced it. Here's the initializing line:

self.storedTimeZones = [[NSMutableArray alloc] init];

but then you assign a different value:

self.storedTimeZones = [NSKeyedUnarchiver unarchiveObjectWithData:storedData];

and at this point, self.storedTimeZones may be either a non-mutable instance of NSArray , or some entirely different object, or nil . I'd guess it's the latter since no exception is thrown when you call -addObject: . That the count is 0 also makes sense if self.storedTimeZones is nil , since the result of messaging nil is nil or 0 or NO , depending on what type you expect.

All you really need to do to properly diagnose the problem is to examine self.storedTimeZones near your last NSLog statement. Set a breakpoint and look at the contents of that property. Useful commands will be:

po self.storedTimeZones
po [self.storedTimeZones class]

You're trying to assign the object to the entire mutable array, you're not adding a object to it.

That's not the way it works.

Try this:

[self.storedTimeZones addObject: [NSKeyedUnarchiver unarchiveObjectWithData:storedData]];

Or this

[self.storedTimeZones addObjectsFromArray:[NSKeyedUnarchiver unarchiveObjectWithData:storedData]];

Or this

self.storedTimeZones = [NSArray alloc]; // Note: not a mutable array [self.storedTimeZones initWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:storedData]];

You've never told us if the contents of the keyedUnarchiver is an array, but we expect it is.

In either case, if this is the only time you add info to the array, you don't need to make it mutable.

Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM