【问题标题】:search in an nsarray of nsdictionaries performance memory optimised way搜索 nsarray 的 nsdictionaries 性能内存优化方式
【发布时间】:2014-09-01 22:40:08
【问题描述】:

这个问题是基于性能的,我得到了想要的结果。 我有一系列这样的字典:

Printing description of arrAppointmentDictionary:
<__NSArrayM 0x16f962a0>(
{
"component_id" = 159;
total = 1;
},
{
"component_id" = 165;
total = 1;
},
{
"component_id" = 177;
total = 1;
},
{
"component_id" = 191;
total = 1;
},
{
"component_id" = 193;
total = 1;
}
)

我根据这样的键在字典中搜索:

for (int i = 0; i<arrAppointmentDictionary.count; i++)
{
    NSMutableDictionary *appointmentDictionary = [arrAppointmentDictionary objectAtIndex:i];
    NSArray *keys = [appointmentDictionary allKeys];
    for (NSString *key in keys)
    {
        @autoreleasepool {
            NSLog(@"Key is %@", key);
            if([[appointmentDictionary objectForKey: key] isEqualToString:[rs stringForColumn:@"id"]])
            {
                layout.numberOfAppointments = [appointmentDictionary objectForKey: @"total"];
                NSLog(@"number of appointments are >>>>>>>>>>>>>>>>> %@", [appointmentDictionary objectForKey: @"total"]);
            }
        }
    }
}

我得到准确的结果。 如何提高这个 for 循环的性能/内存优化,因为它是从另一个 while 循环调用的。

感谢和问候。

【问题讨论】:

    标签: ios performance memory nsmutablearray nsmutabledictionary


    【解决方案1】:

    这是一种方法:

    NSString *wantedId = [rs stringForColumn:@"id"];
    for (NSMutableDictionary *appointmentDictionary in arrAppointmentDictionary) {
        if ([appointmentDictionary[@"id"] isEqualToString:wantedId]) {
            layout.numberOfAppointments = appointmentDictionary[@"total"];
            NSLog(@"number of appointments are >>>>>>>>>>>>>>>>> %@", appointmentDictionary[@"total"]);
            break;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-31
      • 2014-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-05
      • 1970-01-01
      相关资源
      最近更新 更多