supermarin / ObjectiveSugar

ObjectiveC additions for humans. Ruby style.
MIT License
2.17k stars 190 forks source link

NSArray+ObjectiveSugar: Adding mapSelector:(SEL)selector which perform t... #78

Closed huguesbr closed 10 years ago

huguesbr commented 10 years ago

Adding mapSelector:(SEL)selector which perform the selector on each object (behave like map(&:something) in ruby)

supermarin commented 10 years ago

@huguesbr thanks for the contribution.

This already exists in objc - it's called valueForKeyPath:. So you can write this as:

// your example
[[[sampleArray mapSelector:@selector(uppercaseString)] should] equal:@[ @"FIRST", @"SECOND", @"THIRD" ]];

// builtin
[[[sampleArray valueForKeyPath:@"uppercaseString"] should] equal:@[ @"FIRST", @"SECOND", @"THIRD" ]];
huguesbr commented 10 years ago

haha, yes I realize this just after pushing. I wasn't sure that kvc will work as flexible as a selector, which we could extend using [array mapSelector:@selector(compare:) withObject:x]

supermarin commented 10 years ago

@huguesbr in that case it doesn't make sense anymore to stringly type, the block version is more explicit and readable