Is there a way to duplicate the number of NSDictionaries in an array?

只谈情不闲聊 提交于 2019-12-12 06:07:13

问题


I currently have this array of NSDictionarys in my table view, but I need to duplicate the number of dictionaries in the array as the table view scrolls down. Is there a way to achieve this, and also specify or know what the dictionary names will be?

NSMutableDictionary *d0 = [[NSMutableDictionary alloc] init];

NSMutableDictionary *d1 = [[NSMutableDictionary alloc] init];

NSMutableDictionary *d2 = [[NSMutableDictionary alloc] init];

NSMutableDictionary *d3 = [[NSMutableDictionary alloc] init];

NSMutableDictionary *d4 = [[NSMutableDictionary alloc] init];


objects = [NSMutableArray arrayWithObjects:d0, d1, d2, d3, d4, nil];

回答1:


First of all, it seams to be easier to create the dictionaries in a loop:

NSMutableArray *objects = [NSMutableArray new];
for( int i = 0; i < 5; i++ )
{
   [objects addObject:[NSMutableDictionary new]];
}

If you want to "duplicate" the dictionaries, you can do that with a loop, too:

for( NSMutableDictionary *dictionary in [objects copy])
{
  [objects addObject:[dictionary mutableCopy]];
}

But I cannot get rid of the feeling that your whole approach is wrong. What is the idea behind that?



来源:https://stackoverflow.com/questions/32239656/is-there-a-way-to-duplicate-the-number-of-nsdictionaries-in-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!