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

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

问题描述

我有一个具有 NSMutableArray 属性的 Obj-C 2.0 类.如果我使用下面的代码,那么合成的 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;

Apple 没有实现以下语法有什么原因吗?

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

@property (readwrite, mutablecopy) NSMutableArray *myArray;

既然我们没有 mutablecopy,那么处理这种(看似常见的)情况的最佳方法是什么?我应该编写自己的 setter 来执行 -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?

推荐答案

如前所述,正确的方法不是让可变数组成为一个属性.有一个很好的解释说明你应该实现什么才能符合 KVC 这里.

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