[英]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. 我是Objective C的新手,我正在尝试将对象添加到NSMutableArray中,以便可以在项目中多次使用。 I have a Model class as 我有一个Model类
History.h 历史记录
@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. 这是我想用来将对象添加到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. 每次调用pushToArray:...
您都在用一个新的,空的,可变的数组替换现有的itemHistory
。 You'll only ever see the last item to be pushed in that array. 您只会看到该数组中要推送的最后一项。
However, you also don't need to lazily initialize _itemHistory
. 但是,您也不需要延迟初始化_itemHistory
。 Just create an instance in your init
method and be done with it. 只需在init
方法中创建一个实例并完成该操作即可。 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: 并从-pushToArry:...方法中删除它:
//self.itemHistory = [[NSMutableArray alloc] init];
If it still doesn't work, you need to show how you are logging the failure. 如果仍然无法正常工作,则需要显示如何记录故障。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.