在Objective-C中,没有泛型,事实上这只是与Swing一起提供的。 所以你的数据结构只是一个经典的NSArray
NSDictionary
。
如果你想声明这个({ "13/9/2014":("abc","def","ghi") }, { "14/9/2014":("abc") }, { "15/9/2014":("abc","def") })
在objC你着y那样做
NSMutableArray* result = [NSMutableArray new];
NSDictionary * dict = [NSDictionary new];
// With Modern objective C syntax, it would be like that :
dict["13/9/2014"] = @{"abc","def","ghi"};
dict["14/9/2014"] = @{"abc"};
dict["15/9/2014"] = @{"abc","def"};
[result addObject:dict];
不过,当然,你可以为每个值中间NSMutableArray
和使用的NSDictionary
setValue:ForKey:
方法将它添加到字典中。
编辑:添加算法解析DB答案
NSArray*DBAnswer; // this is your array containing the answer from the DB
NSDictionary*result=[NSDictionary new];
for(NSDictionary*d in DBAnswer)
{
NSMutableArray*list;
if(![result containsKey:d["date"]])
{
list = [NSMutableArray new];
result[d["date"]] = list;
}
else
{
list = result[d["date"]];
}
[list addObject:d["title"]];
}
// After that you have the structure in the result NSDictionary
你的目标结构有点傻。你应该有一个按日期键入的字典,条目是标题数组(如果你想要上述结构,你将不得不经过这个结构,作为一个中间步骤。) – 2014-09-13 12:25:14