Created
February 16, 2014 17:42
-
-
Save orta/9037851 to your computer and use it in GitHub Desktop.
Check all view controllers to see if they load the view on init
This file contains 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
// | |
// ARAllViewControllers.m | |
// Artsy | |
// | |
// Created by Orta on 16/02/2014. | |
// Copyright (c) 2014 Art.sy. All rights reserved. | |
// | |
#import <XCTest/XCTest.h> | |
#import "ARClassGetSubclasses.h" | |
@interface ARAllViewControllers : XCTestCase | |
@end | |
@implementation ARAllViewControllers | |
- (void)testAllViewControllerSubclassesDoNotCallViewLoadOnInit | |
{ | |
NSArray *classes = ARClassGetSubclasses(UIViewController.class); | |
for (Class klass in classes) { | |
NSString *klassName = NSStringFromClass(klass); | |
if (![klassName hasPrefix:@"AR"]) continue; | |
@try { | |
UIViewController *controller = [[klass alloc] init]; | |
NSLog(@"Loaded: %d - %@", controller.isViewLoaded, klassName); | |
} | |
@catch (NSException *exception) { | |
} | |
}} | |
@end |
This file contains 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
// | |
// ARClassGetSubclasses.h | |
// Artsy | |
// | |
// Created by Orta on 16/02/2014. | |
// Copyright (c) 2014 Art.sy. All rights reserved. | |
// | |
extern NSArray *ARClassGetSubclasses(Class parentClass); |
This file contains 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
// | |
// ARClassGetSubclasses.m | |
// Artsy | |
// | |
// Created by Orta on 16/02/2014. | |
// Copyright (c) 2014 Art.sy. All rights reserved. | |
// | |
// https://gist.github.com/d4rkl1gh7/1075882 | |
#import "ARClassGetSubclasses.h" | |
#import <Foundation/Foundation.h> | |
#import <objc/objc-runtime.h> | |
NSArray *ARClassGetSubclasses(Class parentClass) { | |
int numClasses = objc_getClassList(NULL, 0); | |
Class *classes = NULL; | |
classes = malloc(sizeof(Class) * numClasses); | |
numClasses = objc_getClassList(classes, numClasses); | |
NSMutableArray *result = [NSMutableArray array]; | |
for (NSInteger i = 0; i < numClasses; i++) { | |
Class superClass = classes[i]; | |
do{ | |
superClass = class_getSuperclass(superClass); | |
} while(superClass && superClass != parentClass); | |
if (superClass == nil) { | |
continue; | |
} | |
[result addObject:classes[i]]; | |
} | |
free(classes); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment