Mads K
Mads K

Reputation: 837

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 i i 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".

Upvotes: 2

Views: 128

Answers (2)

Maulik
Maulik

Reputation: 19418

If you don't need the loop then :

self.breakfast = [NSMutableArray arrayWithArray:meals];

Upvotes: 1

Shubhank
Shubhank

Reputation: 21805

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);

Upvotes: 15

Related Questions