SortUsingDescriptors将NSMutableArray转换为NSMutableDictionaries不可变

时间:2014-02-10 01:12:23

标签: ios objective-c nsmutablearray nsmutabledictionary nssortdescriptor

这有什么办法吗?这是我的代码,这是有效的,但如果我在sortUsingDescriptor下面移动“setObject:YES forKey:”,我会得到“将变异方法发送到不可变对象”

    [dateFormat setDateFormat:@"EEE MMM dd HH:mm:ss ZZZ yyyy"];
    for (NSMutableDictionary *tempDict in tweetArray){
        NSDate *date = [dateFormat dateFromString:[NSString stringWithFormat:@"%@", [tempDict valueForKey:@"created_at"]]];
        NSString *dateString = [NSString stringWithFormat:@"%f", [date timeIntervalSince1970]];

        NSMutableDictionary *newDict = [[NSMutableDictionary alloc]initWithDictionary:tempDict];
        [newDict setValue:dateString forKey:@"created_time"];
        [newDict setValue:@"twitter" forKey:@"network"];
        [combinedPosts addObject:newDict];
    }
    for (NSMutableDictionary *tempDict in instagramArray){
        [combinedPosts addObject:tempDict];
    }

    [[combinedPosts objectAtIndex:0] setObject:@"yes" forKey:@"isDownloadingPhoto"];
    NSSortDescriptor *sortByDate = [NSSortDescriptor sortDescriptorWithKey:@"created_time" ascending:NO];
    [combinedPosts sortUsingDescriptors:[NSArray arrayWithObjects:sortByDate, nil]];

1 个答案:

答案 0 :(得分:0)

你错了。 sortUsingDescriptors在可变数组上运行。方法“sortedArrayUsingDescriptors”使用接收器的已排序内容创建一个新的不可变数组。

在这两种方法中,正在排序的对象将保持不变。 (如果它们是可变的,它们在排序后仍然是可变的。)