makeObjectsPerformSelector: [英] makeObjectsPerformSelector:

查看:25
本文介绍了makeObjectsPerformSelector:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让数组中的所有对象都执行一个选择器.我发现了适当命名的 makeObjectsPerformSelector: 方法,但我对它有疑问.如果我在数组上使用它,它会更改现有数组还是返回一个新数组?如果它修改了现有对象,那么返回一个应用了选择器的新数组的最简单方法是什么?

I want to make all objects in an array perform a selector. I've discovered the appropriately named makeObjectsPerformSelector: method, but I have a question with it. If I use it on an array, will it change the existing array or return a new one? If it modifies the existing object, what be the easiest way to return a new array with the selector applied?

推荐答案

makeObjectsPerformSelector: 将针对数组中的每个对象运行该选择器.如果这些对象被选择器修改,它们将被修改.它不返回任何东西.现在,有一个问题,默认情况下 Cocoa 中的大多数副本都是浅副本,这意味着您获得了一个新数组,但它指向的底层对象是相同的对象.您将需要使用 initWithArray:copyItems 使其也复制根级别的项目.如果您想要一个包含更改对象的新数组以及旧数组,请执行以下操作:

makeObjectsPerformSelector: is going to run that selector against every object in the array. If those objects are modified by the selector they will be modified. It does not return anything. Now, there is a catch, which that by default most copies in Cocoa are shallow copies, which means you get a new array, but the underlying objects it points to are the same objects. You will need to use initWithArray:copyItems to make it copy the root level items as well. If you want a new array containing the altered objects as well as the old array do something like this:

NSArray *newArray = [[NSArray alloc] initWithArray:oldArray copyItems:YES];
[newArray makeObjectsPerformSelector:@selector(doSomethingToObject)];

这篇关于makeObjectsPerformSelector:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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