什么是属性复制意味着在Cocoa的框架?(像UITabBar的items属性) [英] what's property copy means in Cocoa's Framework?(like UITabBar's items property)

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

问题描述

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

In UITabBar.h, a propery signed copy

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

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

这是一个数组
copy是什么意思?
copy NSArray container obj?
copy每个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);



result



bar:0x96a9750,1

result

bar:0x96a9750,1

项目:0x96aa230,2

item:0x96aa230,2

array:0x96aa280,1

array:0x96aa280,1

该项目:0x96aa230,2

that item:0x96aa230,2

testBar.items:0x96aa280,6

testBar.items:0x96aa280,6

推荐答案

在这种情况下,没有复制的原因是 NSArray immutable 。你不需要做一个副本,以防止对数组的更改,因为这样的更改不能做;

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天全站免登陆