Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 407
Mads K Avatar asked Feb 06 '26 02:02

Mads K


2 Answers

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);
like image 176
Shubhank Avatar answered Feb 07 '26 18:02

Shubhank


If you don't need the loop then :

self.breakfast = [NSMutableArray arrayWithArray:meals];

like image 39
Maulik Avatar answered Feb 07 '26 18:02

Maulik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!