简体   繁体   中英

Using custom method to add objects to NSMutableArray Objective C

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.

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.

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