Set bool property of all objects in the array

前端 未结 1 622
甜味超标
甜味超标 2020-12-20 02:07

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

@interface PhotoItem : NSObject

/*!
         


        
1条回答
  •  死守一世寂寞
    2020-12-20 02:43

    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.

    0 讨论(0)
提交回复
热议问题