Cocoa 框架中的属性副本是什么意思?(如 UITabBar 的 items 属性) [英] what's property copy means in Cocoa's Framework?(like UITabBar's items property)

查看:23
本文介绍了Cocoa 框架中的属性副本是什么意思?(如 UITabBar 的 items 属性)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 UITabBar.h 中,一个属性签名的副本

In UITabBar.h, a propery signed copy

@property(nonatomic,copy) NSArray *items;//获取/设置可见

@property(nonatomic,copy) NSArray *items; // get/set visible

这是一个数组复制"是什么意思?复制 NSArray 容器 obj?复制每个 obj NSArray 包含的内容?什么的.

It's a array And what "copy" means? copy NSArray container obj? copy every obj NSArray contains? or something.

UITabBar* testBar = [[UITabBar alloc] init];
UITabBarItem* item = [[UITabBarItem alloc] init];
NSArray* array = [[NSArray alloc] initWithObjects:item, nil];

NSLog(@"bar:%p,%d", testBar, testBar.retainCount);
NSLog(@"item:%p,%d", item, item.retainCount);
NSLog(@"array:%p,%d", array, array.retainCount);

testBar.items = array;

NSLog(@"that item:%p,%d", [testBar.items lastObject], [[testBar.items lastObject] retainCount]);
NSLog(@"testBar.items:%p,%d", testBar.items, testBar.items.retainCount);

结果

栏:0x96a9750,1

result

bar:0x96a9750,1

项目:0x96aa230,2

item:0x96aa230,2

数组:0x96aa280,1

array:0x96aa280,1

那个项目:0x96aa230,2

that item:0x96aa230,2

testBar.items:0x96aa280,6

testBar.items:0x96aa280,6

为什么容器数组和数组中的obj都没有被复制"?

why neither container array nor obj in array has been "copied"?

推荐答案

这种情况下没有复制的原因是NSArray不可变的.您不需要复制它来防止对数组进行更改,因为无法进行此类更改;保留相同的不可变数组就足够了.

The reason the copy has not been made in this case is that NSArray is immutable. You do not need to make a copy of it to guard against changes to the array, because such changes cannot be made; it is sufficient to retain the same immutable array.

如果你用 NSMutableArray 尝试这个实验,你会得到不同的结果.

If you try this experiment with NSMutableArray, you will get a different result.

这篇关于Cocoa 框架中的属性副本是什么意思?(如 UITabBar 的 items 属性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆