-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassProperty.m
78 lines (69 loc) · 2.35 KB
/
ClassProperty.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// ClassProperty.m
//
// Created by Nick Randall on 9/5/20.
// Copyright © 2020 Nick Randall. All rights reserved.
//
#import "ClassProperty.h"
#import "TypeEncoding.h"
@implementation ClassProperty
@synthesize getter = _getter;
@synthesize setter = _setter;
- (instancetype)initWithProperty:(objc_property_t)property {
self = [super init];
if (self) {
_name = @(property_getName(property));
_setterType = Assign; // this is the default
NSString *attribStr = @(property_getAttributes(property));
NSArray *attribs = [attribStr componentsSeparatedByString:@","];
for (NSString *attrib in attribs) {
unichar attribChar = [attrib characterAtIndex:0];
NSString *attribDetail = [attrib substringFromIndex:1];
switch (attribChar) {
case 'T':
_type = [[TypeEncoding alloc] initWithEncoding:attribDetail];
break;
case 'R':
_isReadOnly = YES;
break;
case 'N':
_isNonAtomic = YES;
break;
case 'D':
_isDynamic = YES;
break;
case '&':
_setterType = Strong;
break;
case 'W':
_setterType = Weak;
break;
case 'C':
_setterType = Copy;
break;
case 'G':
_getter = NSSelectorFromString(attribDetail);
break;
case 'S':
_setter = NSSelectorFromString(attribDetail);
break;
case 'V':
_ivarName = attribDetail;
break;
default:
NSAssert(NO, @"Unknown attribute: %@", attrib);
}
}
if (_getter == NULL) {
_getter = NSSelectorFromString(_name);
}
if (_setter == NULL && _isReadOnly == NO) {
NSString *name = [NSString stringWithFormat:@"set%@%@:",
[_name substringToIndex:1].uppercaseString,
[_name substringFromIndex:1]];
_setter = NSSelectorFromString(name);
}
}
return self;
}
@end