I’m trying to save an NSArray as a value in an NSMutableDictionary. When I try to fetch what I’ve added, it returns an NSFCArray instead of an NSArray:
NSArray * list = [NSArray arrayWithObjects:@"one", @"two", @"three", nil];
NSMutableDictionary * dictionary = [[NSMutableDictionary alloc] init];
for (NSString *name in list) {
// first attempt
[dictionary setObject:[NSMutableArray arrayWithObject:@"test"] forKey:name];
NSLog(@"first attempt class: %@", [[dictionary valueForKey:name] class]);
// second attempt
NSArray * realArray = [[NSArray alloc] initWithArray:[dictionary valueForKey:name]];
NSLog(@"second attempt class: %@", [realArray class]);
}
I assume this is being converted for efficiency. However, I need my value returned as an NSArray to call methods like “count” that NSFCArray doesn’t have. If I can’t store values in a dictionary as an NSArray, how can I convert to a NSArray from a NSFCArray?
NSCFArray==NSArray, it’s exposing the “toll-free bridged“ side of Foundation objects.You can do everything with an
NSCFArrayas you can with anNSArray, and vice versa. You can pass anNSArrayto aCFArrayfunction and you can pass aCFArrayto anNSArraymethod.This is because the internal datatype that is known to us as
NSArrayorCFArrayis actually aNSCFArray.This doesn’t apply to all Foundation objects, here is a list of all toll-free bridged objects in Cocoa: