我想设置一个名称,价格,参考......作为属性的NSMutableArray。
像这样:
NSMutableArray *items;
NSString *name;
NSString *price;
NSString *reference;
然后以这种方式将它们添加到数组中:
items[0].name = @"Oven-3000";
items[0].price = @"200€";
items[0].reference = @"231323-1";
items[0].name = @"Oven-3050";
items[0].price = @"250€";
items[0].reference = @"231312-1";
items[0].name = @"Oven-3200";
items[0].price = @"210€";
items[0].reference = @"900023-1";
THX。
答案 0 :(得分:1)
我建议使用特殊的NSObject
子类。
但如果您不想这样做,可以使用NSDictionary
:
NSMutableArray *array = [NSMutableArray array];
[array addObject:@{@"name": @"Oven-3000",
@"price": @"200€",
@"reference": @"900023-1"}];
[array addObject:@{@"name": @"Oven-3050",
@"price": @"250€",
@"reference": @"900023-1"}];
[array addObject:@{@"name": @"Oven-3200",
@"price": @"210€",
@"reference": @"900023-1"}];
访问:
array[0][@"name"];
array[0][@"price"];
array[0][@"reference"];
答案 1 :(得分:1)
如果你想采用NSObject路线,就可以这样做。
首先从NSObjectClass创建一个新文件,并将其命名为ItemObject:
现在在你的.H文件中创建属性(名称,价格,参考)并添加方法:
在ItemObject类的.M文件中添加此方法体:
你的NSObject现在已经完成了!要在任何ViewController中使用它,请在de .M文件中添加#import“ItemObject”:
要创建一个对象,请填充它,将其添加到数组并使用以下方法读取它:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//create objects
ItemObject * item = [[ItemObject alloc]initWithName:@"name1" andPrice:@"price1" andReference:@"reference1"];
ItemObject * item2 = [[ItemObject alloc]initWithName:@"name2" andPrice:@"price2" andReference:@"reference2"];
ItemObject * item3 = [[ItemObject alloc]initWithName:@"name3" andPrice:@"price3" andReference:@"reference3"];
//edit properties
item.name = @"changedTheName";
item.price = @"changedThePrice";
item.reference = @"changedTheReference";
//add to NSMutableArray
NSMutableArray * mutableArray = [NSMutableArray arrayWithObjects:item, item2, item3, nil];
//reading from array
ItemObject * itemRetrieved = mutableArray[0]; //first object
//logging content
NSLog(@"itemRetrieved.name:%@",itemRetrieved.name);
}
日志将显示:
答案 2 :(得分:0)
使用NSDictionary
准备您的数据,如下所示。
NSMutableArray *items=[[NSMutableArray alloc] init];
[items addObject:@{@"name":@"Oven-3000",@"price":@"200€",@"reference":@"231323-1"];
[items addObject:@{@"name":@"Oven-3050",@"price":@"250€",@"reference":@"231312-1"];
[items addObject:@{@"name":@"Oven-3200",@"price":@"210€",@"reference":@"900023-1"];
然后获取
for(NSDictionary *dic in items){
NSLog(@"Name = %@, Price = %@, Reference = %@",[dic objectForKey:@"name"],[dic objectForKey:@"price"],[dic objectForKey:@"reference"])
}
另一种方式 根据评论中的建议,创建自己的属性。 只需创建一个NSObject类型的类,然后添加属性,然后分配。
MyClass.h
@interface MyClass:NSObject
@property(nonatomic, strong) NSString *name;
@property(nonatomic, strong) NSString *price;
@property(nonatomic, strong) NSString *reference;
@end
MyClass.m
@implementation MyClass
@end
然后
MyClass *obj=[[MyClass alloc] init];
obj.name=@"Oven-3000";
obj.price=@"200€";
obj.reference=@"900023-1";
[items addObject:obj];
答案 3 :(得分:0)
创建新文件名是NSObject
的产品子类
在Product.h
文件中
#import <Foundation/Foundation.h>
@interface Product : NSObject
@property (nonatomic, retain) NSString *name; //your product name ,arc u use "strong" in place of "retain"
@property (nonatomic, retain) NSString *price; //product price
@property (nonatomic, retain) NSString *reference;//product reference
- (id)initWithProductName:(NSString *)Productname productPrice:(NSString *)Productprice withProdctReference:(NSString *)Productreference; //this is the initilizer for your product object
@end
<{1>}文件中的
Product.m
在其他一些课程中你需要创建Product的对象,然后只需 #import "Product.h"
@implementation Product
@synthesize name;
@synthesize price;
@synthesize reference;
//initilise the product object
- (id)init
{
self = [super init];
if(self)
{ //because u dont hav any values for it
self.name = nil;
self.price = nil;
self.reference = nil;
}
return self;
}
//initilise the objec with details
- (id)initWithProductName:(NSString *)Productname productPrice:(NSString *)Productprice withProdctReference:(NSString *)Productreference
{
self = [super init];
if(self)
{
self.name = Productname;
self.price = Productprice;
self.reference = Productreference;
}
return self;
}
@end
如下所示
#import "Product.h"
答案 4 :(得分:0)
我认为这是Objective-c方式中最接近的答案:
@interface Item : NSObject
@property NSString *name, *price, *reference;
@end
@implementation Item
- (NSString *)description {
return [NSString stringWithFormat:@"{name: %@, price: %@, reference: %@}", self.name, self.price, self.reference];
}
@end
@interface ItemCollection : NSObject {
NSMutableArray *items;
}
- (Item *)objectAtIndexedSubscript:(NSUInteger)idx NS_AVAILABLE(10_8, 6_0);
- (void)setObject:(Item *)obj atIndexedSubscript:(NSUInteger)idx NS_AVAILABLE(10_8, 6_0);
@end
@implementation ItemCollection
- (id)init {
if ((self = [super init])) {
self->items = [[NSMutableArray alloc] init];
}
return self;
}
- (Item *)objectAtIndexedSubscript:(NSUInteger)idx {
return [self->items objectAtIndexedSubscript:idx];
}
- (void)setObject:(Item *)obj atIndexedSubscript:(NSUInteger)idx {
[self->items setObject:obj atIndexedSubscript:idx];
}
- (NSString *)description {
return [self->items description];
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
ItemCollection *items = [[ItemCollection alloc] init];
items[0] = [[Item alloc] init];
items[0].name = @"Apple";
items[0].price = @"$0.99";
items[0].reference = @"???";
items[1] = [[Item alloc] init];
items[1].name = @"Orange";
items[1].price = @"$0.99";
items[1].reference = @"AAA";
NSLog(@"%@", items);
}
return 0;
}
还有其他一些方法,例如使用struct 数组或使用指针数组,但我不能让它们在ARC环境中工作。