1

我想获取我存储到我的核心数据对象之一的信息并将此数据存储到NSArray对象NSDictionary中,以便我可以将此数组发送到我的一个视图并在表格视图中显示数据。

但是我不太确定如何NSDictionary进入NSArray,这是我的 fetch 请求,希望有人可以帮我把它NSDictionary放入NSArray.. 我不知道如何做到这一点,因为我是如何循环的核心数据对象。

// Test listing all FailedBankInfos from the store
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Manuf" inManagedObjectContext:context];
        [fetchRequest setEntity:entity];

        NSError *error; 

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

        NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
        for (Manuf *manuf in fetchedObjects) {
            [tempManufacturerDictionary setObject:manuf.hasMod forKey:@"HASMOD"];
            [tempManufacturerDictionary setObject:manuf.isLocked forKey:@"ISLOCKED"];
            [tempManufacturerDictionary setObject:manuf.isReg forKey:@"ISREG"];
            [tempManufacturerDictionary setObject:manuf.main forKey:@"MAIN"];

            // How do I put the Dictionary above into an array?
        }

任何帮助,将不胜感激。

4

2 回答 2

4

NSMutableArray您可以在进入循环之前实例化一个,并NSMutableDictionary在每次迭代期间声明一个新的,如下所示:

    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
    NSMutableArray *array = [NSMutableArray array];
    for (Manuf *manuf in fetchedObjects) {
        NSMutableDictionary *tempManufacturerDictionary = [NSMutableDictionary dictionary];
        [tempManufacturerDictionary setObject:manuf.hasMod forKey:@"HASMOD"];
        [tempManufacturerDictionary setObject:manuf.isLocked forKey:@"ISLOCKED"];
        [tempManufacturerDictionary setObject:manuf.isReg forKey:@"ISREG"];
        [tempManufacturerDictionary setObject:manuf.main forKey:@"MAIN"];

        [array addObject:tempManufacturerDictionary];
    }

您还可以将resultTypefetch 请求设置为,NSDictionaryResultType并首先从 fetch 请求中获取字典数组。

于 2012-09-27T23:01:17.130 回答
2

就像您使用任何其他对象一样,使用addObject.

您需要使用NSMutableArray虽然,而不是正常NSArray才能添加元素。如果您严格需要不可变版本,您可以稍后将可变数组转换为普通数组。

于 2012-09-27T23:00:31.610 回答