插入字典后 NSArray 变成 NSCFArray

发布于 2024-10-06 16:49:08 字数 776 浏览 0 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我很OK 2024-10-13 16:49:08

NSCFArray == NSArray,它暴露了"基金会对象的免费桥接端。

您可以使用 NSCFArray 完成所有操作,就像使用 NSArray 一样,反之亦然。您可以将 NSArray 传递给 CFArray 函数,也可以将 CFArray 传递给 NSArray 方法。

这是因为我们称为 NSArrayCFArray 的内部数据类型实际上NSCFArray

这并不适用于所有 Foundation 对象,这里是 Cocoa 中所有免费桥接对象的列表:

alt text

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文