0

I am trying to add objects to a NSMutableArray from another class and it won't work. It works perfectly for the other attribute.

I looked at similar questions but couldn't find a suitable answer.

The object iPack :

@interface IPack : NSObject

     @property float price;
     @property NSMutableArray *cocktails;

@end

In the class of my collection view :

- (void)viewDidLoad {
    [super viewDidLoad];

    self.iPack = [[IPack alloc] init];
    self.iPack.cocktails = [[NSMutableArray alloc] init];

In the class of my cell :

self.collectionView.iPack.price = self.price //perfectly works

NSArray* cock = [NSArray arrayWithObjects:c1,c2,c3,c4,c5, nil];
[self.collectionView.iPack.cocktails addObjectsFromArray:cock]; //line won't work
4
  • Define won't work. Commented Mar 24, 2015 at 11:07
  • what do you mean by "Doesn't work"? Doesn't compile? Doesn't do what you expect? have you checked that the view has actually been loaded, i.e. that the cocktails property is not still nil. Commented Mar 24, 2015 at 11:07
  • [self.iPack.cocktails addObjectsFromArray:cock]; Commented Mar 24, 2015 at 11:10
  • It seems your self.collectionView.iPack.cocktails is nil. Commented Mar 24, 2015 at 11:16

2 Answers 2

3

You haven't shown the [IPack init] method, but I strongly suspect you are not allocating the cocktails array. Simply defining it as a property does not mean it's automatically allocated:

@implementation IPack

- (instancetype)init
{
    self = [super init];
    if (self) {
        _price = 0.0f;
        _cocktails = [NSMutableArray new];
    }
    return self;
}

@end

EDIT

I've just seen this line in your question:

self.iPack.cocktails = [[NSMutableArray alloc] init];

Which would suggest my answer is wrong (despite it being the better way of doing the same thing). Sorry about that; I cannot see why your code doesn't work. Are you sure you are checking correctly?

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

1 Comment

Yes, I am checking through a breakpoint... It is quite an annoying problem and I can't find another solution :/
0

try this,

in IPack class add a method

@interface IPack : NSObject

     @property float price;
     @property NSMutableArray *cocktails;
     - (instancetype)initWithPrice:(float)price cocktails:(NSMutableArray *)cocktails;
@end

and in implementation

@implementtion IPack

- (instancetype)initWithPrice:(float)price cocktails:(NSMutableArray *)cocktails {

    self = [super init];
    if (self) {
        self.price = price;
        self.cocktails = cocktails;
    }
}

@end

In the class of my collection view :

remove alloc init in viewDidLoad that is remove the following lines

self.iPack = [[IPack alloc] init];
self.iPack.cocktails = [[NSMutableArray alloc] init];

In the class of my cell :

NSArray* cock = [NSArray arrayWithObjects:c1,c2,c3,c4,c5, nil];
IPack *ipack = [[IPack alloc] initWithPrice:self.price cocktails:[NSMutableArray arrayArray:cock]];
self.collectionView.iPack = ipack;

Comments

Your Answer

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