Ios 在objective-c中创建数组字典
我正在为孩子们开发应用程序,我需要创建一个数组字典,如{“a”:[苹果、蚂蚁、飞机],“B”:[…],…}。从中,我需要得到苹果的价值,蚂蚁的价值,飞机的价值。 我尝试了以下代码:Ios 在objective-c中创建数组字典,ios,objective-c,xcode,Ios,Objective C,Xcode,我正在为孩子们开发应用程序,我需要创建一个数组字典,如{“a”:[苹果、蚂蚁、飞机],“B”:[…],…}。从中,我需要得到苹果的价值,蚂蚁的价值,飞机的价值。 我尝试了以下代码: alphabetsStoreArray = [[NSMutableArray alloc]initWithObjects:@"a.png",@"b.png",@"c.png",@"d.png",@"e.png",@"f.png",@"g.png",@"h.png",@"i.png",@"j.png",@"k.png
alphabetsStoreArray = [[NSMutableArray alloc]initWithObjects:@"a.png",@"b.png",@"c.png",@"d.png",@"e.png",@"f.png",@"g.png",@"h.png",@"i.png",@"j.png",@"k.png",@"l.png",@"m.png",@"n.png",@"o.png",@"p.png",@"q.png",@"r.png",@"s.png",@"t.png",@"u.png",@"v.png",@"w.png",@"x.png",@"y.png",@"z.png", nil];
但是我需要得到我在下面提到的东西。你可以这样做
NSArray *arrA = [[NSArray alloc]initWithObjects:@"Apple",@"AeroPlane",@"Ant", nil];
NSArray *arrB = [[NSArray alloc]initWithObjects:@"Ball",@"Baloon",@"Banana", nil];
NSArray *arrC = [[NSArray alloc]initWithObjects:@"Cat",@"Car",@"Cow", nil];
NSDictionary *resultDictionary = @{@"A" : arrA, @"B" : arrB, @"C" : arrC} ;
试试这样的
NSDictionary *data = @{@"A":@[@"Apple",@"Ant",.....],
@"B":@[@"Ball",@"Bat",.....],
@"C":@[@"Call",@"Cat",.....],
.
.
.
};
首先,我建议使用一些局部变量(如果这些值是固定的,则使用全局常量)使其更具可读性,但您可以创建一个
NSMutableDictionary
,它将键作为字母表每个值的NSString
。值可以是数组
NSMutableDictionary<NSString *, NSArray<NSString *>> *alphabetDict = [[NSMutableDictionary alloc] init];
// Then you can add each array to the dictionary.
// For example:
NSString *keyA = @"A";
NSArray<NSString *> *arrayForA = @[ @"Aardvark", @"Apple" ];
[alphabetDict setValue:arrayForA forKey:keyA];
// Repeat to add each array to the dict.
NSMutableDictionary*alphabetDict=[[NSMutableDictionary alloc]init];
//然后可以将每个数组添加到字典中。
//例如:
NSString*keyA=@“A”;
NSArray*arrayForA=@[@“土豚”,“苹果”];
[字母表设置值:ArrayForkey:keyA];
//重复此步骤,将每个数组添加到dict。
您可以轻松地在目标c中混搭
NSMutableDictionary *alphabets = [[NSMutableDictionary alloc] init];
alphabets["a"] = @["apple", "ant", "airplane", nil];
aplhabets["b"] = @["boy", "big", nil];
使用此代码
NSMutableArray *arrForA = [[NSMutableArray alloc] initWithObjects:@"Apple", @"Ant", @"Arc", nil];
NSMutableArray *arrForB = [[NSMutableArray alloc] initWithObjects:@"Ball", @"Bat", @"Box", nil];
NSMutableDictionary *dictAlpha = [[NSMutableDictionary alloc] initWithObjectsAndKeys: arrForA, @"A", arrForB, @"B", nil];
NSLog(@"%@",dictAlpha); // 1
NSArray *keys=[dictAlpha allKeys];
for (int i = 0; i< keys.count; i++) {
NSArray *arrVal = [dictAlpha objectForKey:keys[i]];
for (int j=0; j<arrVal.count; j++) {
NSLog(@"%@ for %@", keys[i], arrVal[j]); // 2
}
}
NSMutableArray*arrForA=[[NSMutableArray alloc]initWithObjects:@“Apple”、“Ant”、“Arc”、“nil];
NSMUTABLEARRY*arrForB=[[NSMUTABLEARRY alloc]initWithObjects:@“球”、“蝙蝠”、“盒子”、零];
NSMutableDictionary*dictAlpha=[[NSMutableDictionary alloc]initWithObjectsAndKeys:arrForA、@“A”、arrForB、@“B”、nil];
NSLog(@“%@”,dictAlpha);//1.
NSArray*keys=[dictAlpha-allKeys];
对于(int i=0;i
二,
对不起,代码可能有语法问题!!
-你写这行是为了回答!如果你知道代码中有问题,那么你不应该写答案!而且你的代码在编辑之前也有问题!这就是为什么否决投票!顺便说一句,现在你已经编辑了答案,所以我收回了它!谢谢。但我还需要从中获取苹果的价值要获得值,从这里开始非常简单。只需亲自尝试一下。你所要做的就是从字典中阅读。像这样[data valueForKey:@“a”]你能在if条件下提供吗please@jesti哪个If条件?我需要If语句条件中给出的上述代码u
// A dictionary object
NSDictionary *dict;
// Create array to hold dictionaries
NSMutableArray *arrayOfDictionaries = [NSMutableArray array];
// Create three dictionaries
dict = [NSDictionary dictionaryWithObjectsAndKeys:
// Key value pairs
@"Bock", style,
@"Deep Gold", appearance,
[NSNumber numberWithInt:25], hopProfile,
nil];
arrayOfDictionaries addObject:dict];
dict = [NSDictionary dictionaryWithObjectsAndKeys:
// Key value pairs
@"India Pale Ale (IPA)", style,
@"Copper", appearance,
[NSNumber numberWithInt:50], hopProfile,
nil];
[arrayOfDictionaries addObject:dict];
dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"Stout", style,
@"Jet Black", appearance,
[NSNumber numberWithInt:35], hopProfile,
nil];
[arrayOfDictionaries addObject:dict];
NSLog(@"array of dictionaries: %@", arrayOfDictionaries);