-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSObject+ReflectionAdditions.m
59 lines (44 loc) · 1.28 KB
/
NSObject+ReflectionAdditions.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//
// ObjectSubclassesAddition.m
// This file is part of the "SWApplicationSupport" project, and is distributed under the MIT License.
//
// Created by Samuel Williams on 2/06/05.
// Copyright 2005 Samuel Williams. All rights reserved.
//
#import "NSObject+ReflectionAdditions.h"
#import <objc/objc-runtime.h>
@implementation NSObject (ReflectionAdditions)
+ (NSArray*) subclasses {
NSMutableArray *subclasses;
struct objc_class *superClass;
Class *classes = NULL;
Class *current;
const Class thisClass = [self class];
int count, index;
subclasses = [[NSMutableArray alloc] initWithCapacity:12];
count = objc_getClassList(NULL, 0);
if (count) {
classes = malloc(sizeof(Class) * count);
NSAssert (classes != NULL, @"Memory Allocation Failed in [NSObject +subclasses].");
(void) objc_getClassList(classes, count);
}
if (classes) {
current = classes;
for (index = 0; index < count; ++index, ++current) {
superClass = *current;
while (superClass = (superClass)->super_class)
if (superClass == thisClass) {
[subclasses addObject:*current];
break;
}
}
free(classes);
}
return subclasses;
}
+ (Class) classWithName: (NSString*)clsName {
id obj;
obj = objc_lookUpClass([clsName cString]);
return obj;
}
@end