Last active
September 27, 2015 05:47
-
-
Save 7gano/1220899 to your computer and use it in GitHub Desktop.
makeObjectsPerformSelector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; |
こういう、表示が関係ないコードの場合は、main関数が書いてあるとすぐに実行できていいよね。
以下の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]);
1行でできることを5行で書かれると、ソース読むときに何か意図があるのかな?と思うので
読むやすくしてほしいよねー、というだけだったり。
Blocks対応後は、
enumerateObjectsUsingBlock:
enumerateObjectsWithOptions:usingBlock:
enumerateObjectsAtIndexes:options:usingBlock:
も良さそうです。
それらを使うと、Rubyのmapっぽい感じになりますね。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NSArray -(void)makeObjectsPerformSelector; でNSArrayが持っている全てのObjectに対してmethodを実行できる。