如果您真的坚持将对象存储在字典中,并且如果您正在处理字符串,那么您总是可以将所有字符串附加在一起,用逗号分隔,然后当您从键中检索对象时,您将拥有所有准csv格式的对象!然后,您可以轻松地将该字符串解析为对象数组。
这是您可以运行的一些示例代码:
NSString *forename = @"forename";
NSString *surname = @"surname";
NSString *reminderDate = @"10/11/2012";
NSString *code = @"code";
NSString *dummy = [[NSString alloc] init];
dummy = [dummy stringByAppendingString:forename];
dummy = [dummy stringByAppendingString:@","];
dummy = [dummy stringByAppendingString:surname];
dummy = [dummy stringByAppendingString:@","];
dummy = [dummy stringByAppendingString:reminderDate];
dummy = [dummy stringByAppendingString:@","];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setObject:dummy forKey:code];
然后检索和解析字典中的对象:
NSString *fromDictionary = [dictionary objectForKey:code];
NSArray *objectArray = [fromDictionary componentsSeparatedByString:@","];
NSLog(@"object array: %@",objectArray);
它可能不像dreamlax建议的那样具有多层字典那么干净,但是如果您正在处理一个字典,您想在其中存储一个键的数组并且该数组中的对象本身没有特定的键,这是一个解决方案!