什么是最好的方式使用Obj-C 2.0属性与可变对象,如NSMutableArray? [英] What's the best way to use Obj-C 2.0 Properties with mutable objects, such as NSMutableArray?

查看:119
本文介绍了什么是最好的方式使用Obj-C 2.0属性与可变对象,如NSMutableArray?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Obj-C 2.0类,它有一个NSMutableArray属性。如果我使用下面的代码,那么合成的setter将给我一个不可变的副本,而不是一个可变的副本:

I have an Obj-C 2.0 class that has an NSMutableArray property. If I use the following code, then the synthesised setter will give me an immutable copy, not a mutable one:

@property (readwrite, copy) NSMutableArray *myArray;

有没有理由苹果没有实现以下语法?

Is there any reason that Apple didn't implement the following syntax?

@property (readwrite, mutablecopy) NSMutableArray *myArray;

由于我们没有 mutablecopy 什么是最好的办法来处理这个(貌似常见的)情况?

Since we don't have mutablecopy, what's the best way to handle this (seemingly common) situation? Should I just write my own setter that does a -mutableCopy?

推荐答案

如前所述,正确的方式

As said before, the right way to do it is not to make the mutable array a property. There's a great explanation of what you should implement to be KVC compliant here.

这篇关于什么是最好的方式使用Obj-C 2.0属性与可变对象,如NSMutableArray?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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