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 ?
isSelectedname for the property seems a bit odd. Apple uses a scheme where boolean properties are named with a simple adjective and have a setter name that uses the "is" prefix, like@property (getter=isSelected) BOOL selected;. This is even documented in the KVC guide explaining how keys are resolved.