what#39;s property copy means in Cocoa#39;s Framework?(like UITabBar#39;s items property)(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 属性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Cocoa 框架中的属性副本是什么意思?(如 UITabBar 的 items 属性)


基础教程推荐
- iOS4 创建后台定时器 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01