阵列中的所有对象的设置布尔属性 [英] Set bool property of all objects in the array

查看:144
本文介绍了阵列中的所有对象的设置布尔属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经叫一个模型类 PhotoItem 。在这我有一个 BOOL 属性 isSelected

  @interface PhotoItem:NSObject的/ *!
 *表示照片是否被选中与否
 * /
@属性(非原子,分配)BOOL isSelected;@结束

我有一个的NSMutableArray 持有这一特定模式的对象。我想要做的是,在一个特定的事件中,我想设置的阵列为true或false中的所有对象的布尔值。我能做到这一点是通过遍历数组结束,设置的值。

相反的,我试过用:

  [_ photoItemArray makeObjectsPerformSelector:@selector(setIsSelected :) withObject:[NSNumber的numberWithBool:真]];

但我知道它不会工作,它没有。另外,作为在帕拉姆(因为不会反对型)我无法通过真或假。因此,对于固定这个问题,我实现了如自定义的公共方法:

  / *!
 *用于设置照片选择状态
 * @参数选择:表示选择状态
 * /
- (无效)setItemSelection:(NSNumber的*)选择
{
    _isSelected = [选择boolValue]
}

和调用它喜欢的:

  [_ photoItemArray makeObjectsPerformSelector:@selector(setItemSelection :) withObject:[NSNumber的numberWithBool:真]];

它的工作完美。但我的问题是,有没有更好的方式实现这一目标没有实现自定义的公共方法?


解决方案

  

有没有什么更好的方法来实现这一目标没有实现自定义的公共方法?


这听起来像你所要求的意见,所以这里是我的:保持简单

 为(PhotoItem *在_photoItemArray项)
    item.isSelected = YES;

为什么混淆通过晦涩的方法弯路一个简单的事情时,你可以写code,任何人都会立刻明白?

做同样的事情的另一种方法是:

  [_ photoItemArray的setValue:@YES forKey:@isSelected];

这并不需要额外定制的setter方法​​,因为不KVC拆箱适合你。

不过,我再次将投票反对使用这种结构。我想他们是从简单的意思分散和混乱之后你来开发。

I've a model class called PhotoItem. In which I have a BOOL property isSelected

@interface PhotoItem : NSObject

/*!
 * Indicates whether the photo is selected or not
 */
@property (nonatomic, assign) BOOL isSelected;

@end

I've an NSMutableArray which holds the object of this particular model. What I want to do is, in a particular event I want to set the bool value of all objects in the array to true or false. I can do that by iterating over the array and set the value.

Instead of that I tried using:

[_photoItemArray makeObjectsPerformSelector:@selector(setIsSelected:) withObject:[NSNumber numberWithBool:true]];

But I know it won't work and it didn't. Also I can't pass true or false as the param in that (since those are not object type). So for fixing this issue, I implemented a custom public method like:

/*!
 * Used for setting the photo selection status
 * @param selection : Indicates the selection status
 */
- (void)setItemSelection:(NSNumber *)selection
{
    _isSelected = [selection boolValue];
}

And calling it like:

[_photoItemArray makeObjectsPerformSelector:@selector(setItemSelection:) withObject:[NSNumber numberWithBool:true]];

It worked perfectly. But my question is, Is there any better way to achieve this without implementing a custom public method ?

解决方案

Is there any better way to achieve this without implementing a custom public method?

This sounds like you are asking for opinion, so here is mine: Keep it simple.

for (PhotoItem *item in _photoItemArray)
    item.isSelected = YES;

Why obfuscate a simple thing with detours through obscure methods when you can write code that anybody will immediately understand?

Another way of doing the same thing would be:

[_photoItemArray setValue:@YES forKey:@"isSelected"];

This does not need the custom additional setter method because KVC does the unboxing for you.

But again I would vote against using such constructs. I think they are distracting from the simple meaning and confusing developers that come after you.

这篇关于阵列中的所有对象的设置布尔属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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