简体   繁体   中英

Can't add NSMutableArray to NSMutableArray

This must be one of those errors, where you have been staring at the code, for so long, that you can't find the error.

I have this code block, where i loop through a NSMutableArray, containing multiple NSMutableArrays:

//  FoodViewController.m
#import "FoodViewController.h"

@interface FoodViewController ()
@property (strong,nonatomic) NSMutableArray *breakfast;

@end

@implementation FoodViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSArray *meals = [[dbManagerClass getSharedInstance]findCurrentDay];

    for (NSMutableArray *row in meals) {
        [self.breakfast addObject:row];
    }

    NSLog(@"%@",self.breakfast);
}

@end

I can see that ii have something in my *meals, because i get the following from NSLog'ing it:

(
    (
    2,
    Dinner,
    Pizza,
    574,
    "20.03.2014",
    empty
),
    (
    3,
    Breakfast,
    "Buttered toast",
    394,
    "20.03.2014",
    empty
)

But somehow it doesn't get added to the breakfast-NSMutableArray, as NSLog returns "null".

You dont initialize breakfast array before adding.

Do this

self.breakfast = [NSMutableArray array];

for (NSMutableArray *row in meals) {
    [self.breakfast addObject:row];
}

NSLog(@"%@",self.breakfast);

如果你不需要循环那么:

self.breakfast = [NSMutableArray arrayWithArray:meals];

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