开发者

NSArray turned into NSCFArray upon insertion to dictionary

开发者 https://www.devze.com 2023-01-29 23:17 出处:网络
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:

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 co开发者_开发问答nvert 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 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:

NSArray turned into NSCFArray upon insertion to dictionary

0

精彩评论

暂无评论...
验证码 换一张
取 消