插入字典后 NSArray 变成 NSCFArray
我正在尝试将 NSArray 保存为 NSMutableDictionary 中的值。当我尝试获取我添加的内容时,它返回一个 NSFCArray 而不是 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]);
}
我认为这是为了提高效率而进行转换的。但是,我需要将我的值作为 NSArray 返回来调用 NSFCArray 没有的“count”等方法。如果我无法将值作为 NSArray 存储在字典中,如何从 NSFCArray 转换为 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 convert to a NSArray from a NSFCArray?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NSCFArray
==NSArray
,它暴露了"基金会对象的免费桥接”端。您可以使用
NSCFArray
完成所有操作,就像使用NSArray
一样,反之亦然。您可以将NSArray
传递给CFArray
函数,也可以将CFArray
传递给NSArray
方法。这是因为我们称为
NSArray
或CFArray
的内部数据类型实际上是NSCFArray
。这并不适用于所有 Foundation 对象,这里是 Cocoa 中所有免费桥接对象的列表:
NSCFArray
==NSArray
, it's exposing the "toll-free bridged" side of Foundation objects.You can do everything with an
NSCFArray
as you can with anNSArray
, and vice versa. You can pass anNSArray
to aCFArray
function and you can pass aCFArray
to anNSArray
method.This is because the internal datatype that is known to us as
NSArray
orCFArray
is actually aNSCFArray
.This doesn't apply to all Foundation objects, here is a list of all toll-free bridged objects in Cocoa: