Skip to content

Instantly share code, notes, and snippets.

@7gano
Last active September 27, 2015 05:47
Show Gist options
  • Save 7gano/1220899 to your computer and use it in GitHub Desktop.
Save 7gano/1220899 to your computer and use it in GitHub Desktop.
makeObjectsPerformSelector
#import <UIKit/UIKit.h>
int main(int argc, char *argv[])
{
@autoreleasepool {
UIView* superView = [[UIView alloc] initWithFrame:CGRectZero];
for(int i = 0; i < 10; i++){
UIView* view = [[UIView alloc] initWithFrame:CGRectZero];
[superView addSubview:view];
}
NSLog(@"count = %d", [superView.subviews count]);
[superView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
NSLog(@"count = %d", [superView.subviews count]);
}
}
2011-09-16 09:44:47.526 Hoge100[1529:40b] count = 10
2011-09-16 09:44:47.527 Hoge100[1529:40b] count = 0
makeObjectsPerformSelector:
Sends to each object in the array the message identified by a given selector,
starting with the first object and continuing through the array to the last object.
- (void)makeObjectsPerformSelector:(SEL)aSelector
Parameters
aSelector
A selector that identifies the message to send to the objects in the array.
The method must not take any arguments,
and must not have the side effect of modifying the receiving array.
Discussion
This method raises an NSInvalidArgumentException if aSelector is NULL.
Availability
Available in iOS 2.0 and later.
See Also
– makeObjectsPerformSelector:withObject:
Related Sample Code
LazyTableImages
Declared In
NSArray.h
@interface NSArray (NSExtendedArray)
- (void)makeObjectsPerformSelector:(SEL)aSelector;
- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)argument;
@interface NSSet (NSExtendedSet)
- (void)makeObjectsPerformSelector:(SEL)aSelector;
- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)argument;
@7gano
Copy link
Author

7gano commented Sep 16, 2011

NSArray -(void)makeObjectsPerformSelector; でNSArrayが持っている全てのObjectに対してmethodを実行できる。

@7gano
Copy link
Author

7gano commented Sep 16, 2011

こういう、表示が関係ないコードの場合は、main関数が書いてあるとすぐに実行できていいよね。

@monmon
Copy link

monmon commented Sep 16, 2011

以下の2つみたいにループした場合と同じことやってるけどmakeObjectsPerformSelectorは1行で楽という認識になった。
あと複雑な処理の場合はmakeObjectsPerformSelectorの方がスコープが閉じられていいってメリットもあるのかな?

NSLog(@"count = %d", [superView.subviews count]);
for (UIView *view in superView.subviews) {
  [view removeFromSuperview];
}
NSLog(@"count = %d", [superView.subviews count]);
NSLog(@"count = %d", [superView.subviews count]);
NSEnumerator *viewEnumerator = [superView.subviews objectEnumerator];
UIView *view;
while (view = [viewEnumerator nextObject]) {
  [view removeFromSuperview];
}
NSLog(@"count = %d", [superView.subviews count]);

@7gano
Copy link
Author

7gano commented Sep 30, 2011

1行でできることを5行で書かれると、ソース読むときに何か意図があるのかな?と思うので
読むやすくしてほしいよねー、というだけだったり。

@tokorom
Copy link

tokorom commented Oct 2, 2013

Blocks対応後は、

  • enumerateObjectsUsingBlock:
  • enumerateObjectsWithOptions:usingBlock:
  • enumerateObjectsAtIndexes:options:usingBlock:

も良さそうです。

@7gano
Copy link
Author

7gano commented Oct 3, 2013

それらを使うと、Rubyのmapっぽい感じになりますね。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment