-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathNSObject+CleanUpProperties.m
More file actions
75 lines (65 loc) · 2.89 KB
/
NSObject+CleanUpProperties.m
File metadata and controls
75 lines (65 loc) · 2.89 KB
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
//
// NSObject+CleanUpProperties.m
//
// Created by Vincent Gable on 12/4/08.
// vincent.gable@gmail.com
// http://vincentgable.com
// Copyright 2008 Vincent Gable.
// You are free to use this code however you like.
// But I would appreciate hearing what you use it for :-)
//
#import "NSObject+CleanUpProperties.h"
@implementation NSObject(CleanUpProperties)
//syntax is:
//,S(methodNameHere):,
//for example:
//,SintSetFoo:,
//This would be even easier with regex.
//for more information on attribute strings see:
// http://developer.apple.com/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/chapter_8_section_4.html#//apple_ref/doc/uid/TP40008048-CH101-SW5
static NSString* SetterNameFromAttributes(const char *propertyAttrsCString) {
NSString *propertyAttributes = [NSString stringWithUTF8String:propertyAttrsCString];
for(NSString *property in [propertyAttributes componentsSeparatedByString:@","])
if([property hasPrefix:@"S"] && [property hasSuffix:@":"])
return [property substringFromIndex:1];
return nil;
}
static NSString *SetterNameFromPropertyName(const char* propertyNameCStr) {
NSString *propertyName = [NSString stringWithUTF8String:propertyNameCStr];
NSString *firstLetterCapitalized = [[propertyName substringToIndex:1] capitalizedString];
NSString *propertyNameTail = [propertyName substringFromIndex:1];
return [NSString stringWithFormat:@"set%@%@:",firstLetterCapitalized, propertyNameTail];
}
//for more information on attribute strings see:
// http://developer.apple.com/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/chapter_8_section_4.html#//apple_ref/doc/uid/TP40008048-CH101-SW5
static BOOL PropertyIsObjCObject(const char* propertyAttributeString) {
return propertyAttributeString &&
propertyAttributeString[0] == 'T' &&
propertyAttributeString[1] == '@'; //all other pointers have a '^' here
}
//Readonly properties have
//",R,"
//in their attribute strings
//for more information on attribute strings see:
// http://developer.apple.com/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/chapter_8_section_4.html#//apple_ref/doc/uid/TP40008048-CH101-SW5
static BOOL PropertyIsSettable(const char* propertyAttributeString) {
return NULL == strstr(propertyAttributeString, ",R,");
}
- (void) setEveryObjCObjectPropertyToNil;
{
unsigned int i, propertyCount = 0;
objc_property_t *propertyList = class_copyPropertyList([self class], &propertyCount);
if(propertyList){
for(i = 0; i < propertyCount; i++){
const char *propertyAttrs = property_getAttributes(propertyList[i]);
if(PropertyIsObjCObject(propertyAttrs) && PropertyIsSettable(propertyAttrs)) {
NSString *setterName = SetterNameFromAttributes(propertyAttrs);
if(!setterName)
setterName = SetterNameFromPropertyName(property_getName(propertyList[i]));
[self performSelector:NSSelectorFromString(setterName) withObject:nil];
}
}
free(propertyList);
}
}
@end