【问题标题】:Replace objects in NSMutableArray contains NSMutableDictionary with NSIndexSet替换 NSMutableArray 中的对象包含 NSMutableDictionary 和 NSIndexSet
【发布时间】:2014-07-23 11:59:07
【问题描述】:

我有一个主数组 (arrayEvents) 包含事件列表,其中包含 start_date、event_id、event_name 等字段,许多事件可以在同一日期但不同时间发生。所以我只使用唯一的日期(arrayDates)来检查并与当前日期(currentDate 其范围从每月的第一天到每月的最后一天)进行比较,以显示特定日期的事件。我到此为止。现在我的问题是,我需要更新arrayEvents 以供以后比较,所以我应用以下逻辑...but I am in the middle of the sea 并且不知道去哪里!

for(int i=0;i<[arrayDates count];i++) {
    NSDate *obj = [arrayDates objectAtIndex:i];
    if([obj isEqualToDate:currentDate]) {
        //This date has some events, we've done up to here.
        //Now taking indexes of same objects (that's same dates objects) from the arrayEvents
        NSIndexSet *indexesOfObjects = [arrayDates indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
            return [arrayEvents containsObject:obj];
        }];
        if([indexesOfObjects count] > 1) {
            //We found some objects into arrayEvents, now I have to update
            //those objects into arrayEvents.
            //How to achieve this?

            //My plan was to iterate each indexes and make update on arrayEvents 
            //but I can't do this, as I don't have access to each index from NSIndexSet.
        }
    }
}

[[arrayEvents objectsAtIndexes:indexesOfObjects] 
setValue:[arrayDates objectAtIndex:i] forKey:@"filter_date"];

没用。


参考,How to indicate index of NSIndexSet object? 但对我没有帮助。

【问题讨论】:

    标签: ios nsmutablearray comparison nsdictionary nsindexset


    【解决方案1】:

    您可以尝试这样做:

    NSMutableDictionary *dict=[[NSMutableDictionary alloc]initWithDictionary:[arrayEvents objectAtIndex:indexPath.row]];
    
    [dict setValue:[arrayDates objectAtIndex:i] forKey:@"filter_date"];
    
    [arrayEvents replaceObjectAtIndex:indexPath.row withObject:dict];
    

    或者你可以使用

    replaceObjectsAtIndexes:(NSIndexSet *) withObjects:(NSArray *)a
    

    哪个适合您的方便

    【讨论】:

    • 你不要使用 enumerateIndexesUsingBlock:..从索引集中枚举并检索相应的数据,然后修改它,然后替换它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多