-
Notifications
You must be signed in to change notification settings - Fork 3
/
SettingsMapping.m
56 lines (45 loc) · 1.34 KB
/
SettingsMapping.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
@implementation SettingsMapping
+(instancetype)mappingWithName:(NSString*)name getter:(NSString*)getterName setter:(NSString*)setterName defaultValue:(BOOL)defaultValue
{
SettingsMapping* mapping=SettingsMapping.alloc.init.autorelease;
mapping.name=name;
mapping.getter=NSSelectorFromString(getterName);
mapping.setter=NSSelectorFromString(setterName);
mapping.defaultValue=defaultValue;
return mapping;
}
+(instancetype)mappingWithName:(NSString*)name getter:(NSString*)getterName defaultValue:(BOOL)defaultValue
{
NSString* firstChar=[getterName substringToIndex:1].uppercaseString;
NSString* otherChars=[getterName substringFromIndex:1];
NSString* setterName=[NSString stringWithFormat:@"set%@%@:",firstChar,otherChars];
return [SettingsMapping mappingWithName:name getter:getterName setter:setterName defaultValue:defaultValue];
}
-(BOOL)supported
{
return [getXcodeSettings() respondsToSelector:self.getter]&&[getXcodeSettings() respondsToSelector:self.setter];
}
-(BOOL)getValue
{
if(self.supported)
{
return (BOOL)(long)[getXcodeSettings() performSelector:self.getter];
}
return false;
}
-(void)setValue:(BOOL)value
{
if(self.supported)
{
[getXcodeSettings() performSelector:self.setter withObject:(id)(long)value];
}
}
-(void)reset
{
[self setValue:self.defaultValue];
}
-(void)toggle
{
[self setValue:!self.getValue];
}
@end