tassock
tassock

Reputation: 1653

NSArray turned into NSCFArray upon insertion to dictionary

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?

Upvotes: 1

Views: 1766

Answers (1)

Jacob Relkin
Jacob Relkin

Reputation: 163288

NSCFArray == NSArray, it's exposing the "toll-free bridged" side of Foundation objects.

You can do everything with an NSCFArray as you can with an NSArray, and vice versa. You can pass an NSArray to a CFArray function and you can pass a CFArray to an NSArray method.

This is because the internal datatype that is known to us as NSArray or CFArray is actually a NSCFArray.

This doesn't apply to all Foundation objects, here is a list of all toll-free bridged objects in Cocoa:

alt text

Upvotes: 7

Related Questions