简体   繁体   中英

Two Multi Dimensional Array with Objects

I would like to use an NSDictionay (I think this is the correct method) to save data to in a class which is accessed by other classes:

.h

@property (retain, nonatomic) NSArray   *NameKey;
@property (retain, nonatomic) NSArray   *DataFields;
@property (retain, nonatomic) NSDictionary *MDArray;

.m

@synthesize NameKey;
@synthesize DataFields;
@synthesize MDArray;

- (id) init
{
        NameKey = [NSArray arrayWithObjects:@"DeviceName",nil];
        DataFields = [NSArray arrayWithObjects:@"Serial", nil];
        MDArray = [NSDictionary dictionaryWithObjects:DataFields forKeys:NameKey];
}

Couple of questions:

  1. Is this the correct method for setting up an NSDictionary?

  2. How you I add/delete/modify the data. Assuming that the name was unique

Thanks in advance.

Why not use modern syntax:

MDArray = @{ @"DeviceName": @"Serial" }

If you want a mutable version you can send it the mutableCopy message

The way I solved this was to:

devices = [[NSMutableArray alloc] init];
dataFields = [[NSArray alloc] initWithObjects:@"Name",@"Type", nil];

NSArray *addDevice = [[NSArray alloc] initWithObjects:@"MM-TEST",@"-- --", nil];
NSDictionary *infoblock = [[NSMutableDictionary alloc] initWithObjects:addDevice forKeys:dataFields];
[devices addObject:infoblock];

Seems to work fine.

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