0

I am new to Objective C, I'm trying to add objects to NSMutableArray that I can use multiple times on my project. I have a Model class as

History.h

import

@interface History : NSObject

@property (nonatomic) NSString *itemName;
@property (nonatomic) int quantity;
@property (nonatomic) double total;
@property (nonatomic) NSDate *purchaseDate;

- (instancetype)initWithName: (NSString*)itemName withQuantity:(int)quantity withTotal:(double) total withPurchaseDate:(NSDate*) purchaseDate;

@end

History.m

#import "History.h"

@implementation History

-(instancetype)initWithName: (NSString*)iName withQuantity:(int)iQuantity withTotal:(double) iTotal withPurchaseDate:(NSDate*) iPdate {

    self = [super init];
    if(self) {
        self.itemName = iName;
        self.quantity = iQuantity;
        self.quantity = iQuantity;
        self.purchaseDate = iPdate;
    }
    return self;
}

@end

Repository.h

#import <Foundation/Foundation.h>

@interface Repository : NSObject

@property (nonatomic) NSMutableArray *itemHistory;

-(void) pushToArray:(NSString *)name withQuantity:(int)qty withTotal:(double) total withPurchaseDate:(NSDate*) pDate;

@end

Repository.m

#import "Repository.h"
#import "History.h"

@interface Repository()

//@property (nonatomic) NSMutableArray *itemHistory;

@end

@implementation Repository

-(NSMutableArray *) itemHistory {

    if(_itemHistory == nil) {
        _itemHistory = [[NSMutableArray alloc] init];
    }
    return _itemHistory;
}

This is my method that I want to use to add objects to the MutableArray.

-(void) pushToArray:(NSString *)name withQuantity:(int)qty withTotal:(double) total withPurchaseDate:(NSDate*) pDate {

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

    History *obj = [[History alloc] init];
    obj.itemName = name;
    obj.quantity = qty;
    obj.total = total;
    obj.purchaseDate = pDate;

    [self.itemHistory addObject:obj];

}

@end

Thank you for your help in advance.

8
  • Your question is unclear. Please provide details on what you need help with. Commented Oct 10, 2016 at 16:53
  • When I call pushToArray method atempting to add object to MutableArray it doesn't work. I need help how I can fix this. Commented Oct 10, 2016 at 16:56
  • On another Controller I initialize as Commented Oct 10, 2016 at 16:57
  • -(Repository*)myModel { if(_myModel == nil) _myModel = [[Repository alloc]init]; return _myModel; } Commented Oct 10, 2016 at 16:57
  • Remove self.itemHistory = [[NSMutableArray alloc] init]; in the last block of code. Because you just keep creating a new array. Commented Oct 10, 2016 at 16:57

1 Answer 1

1

Every time you call pushToArray:... you are replacing the existing itemHistory with a new, empty, mutable array. You'll only ever see the last item to be pushed in that array.

However, you also don't need to lazily initialize _itemHistory. Just create an instance in your init method and be done with it. Saves confusion and refactoring hell.


In your Repository class, simply implement the designated initializer:

- (instancetype) init
{
    if (self=[super init]) {
       _itemHistory = [[NSMutableArray alloc] init];
    }
    return self;
}

Uncomment the property:

@interface Repository()
@property (nonatomic) NSMutableArray *itemHistory;
@end

And remove this from the -pushToArry:... method:

//self.itemHistory = [[NSMutableArray alloc] init];

If it still doesn't work, you need to show how you are logging the failure.

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

1 Comment

Sorry, I did not understand, could you please show me how I can do that, I'm new to this.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.