Skip to content

Commit 16fd56f

Browse files
author
Sommer, Martin
committed
- added NSDictionary and NSObject categories
1 parent 7aec755 commit 16fd56f

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed

iOSLib.xcodeproj/project.pbxproj

+12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
C541197916DF5BB9004892E9 /* NSDate+Sharp.m in Sources */ = {isa = PBXBuildFile; fileRef = C541197816DF5BB9004892E9 /* NSDate+Sharp.m */; };
2020
C541197C16DF6443004892E9 /* UIScrollView+Sharp.m in Sources */ = {isa = PBXBuildFile; fileRef = C541197B16DF6443004892E9 /* UIScrollView+Sharp.m */; };
2121
C541197F16DF9764004892E9 /* NSArray+Sharp.m in Sources */ = {isa = PBXBuildFile; fileRef = C541197E16DF9764004892E9 /* NSArray+Sharp.m */; };
22+
C541198216E0FE97004892E9 /* NSDictionary+Sharp.m in Sources */ = {isa = PBXBuildFile; fileRef = C541198116E0FE97004892E9 /* NSDictionary+Sharp.m */; };
23+
C541198516E1007B004892E9 /* NSObject+Sharp.m in Sources */ = {isa = PBXBuildFile; fileRef = C541198416E1007B004892E9 /* NSObject+Sharp.m */; };
2224
/* End PBXBuildFile section */
2325

2426
/* Begin PBXFileReference section */
@@ -41,6 +43,10 @@
4143
C541197B16DF6443004892E9 /* UIScrollView+Sharp.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIScrollView+Sharp.m"; path = "Categories/UIScrollView+Sharp.m"; sourceTree = "<group>"; };
4244
C541197D16DF9764004892E9 /* NSArray+Sharp.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSArray+Sharp.h"; path = "Categories/NSArray+Sharp.h"; sourceTree = "<group>"; };
4345
C541197E16DF9764004892E9 /* NSArray+Sharp.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSArray+Sharp.m"; path = "Categories/NSArray+Sharp.m"; sourceTree = "<group>"; };
46+
C541198016E0FE97004892E9 /* NSDictionary+Sharp.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSDictionary+Sharp.h"; path = "Categories/NSDictionary+Sharp.h"; sourceTree = "<group>"; };
47+
C541198116E0FE97004892E9 /* NSDictionary+Sharp.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSDictionary+Sharp.m"; path = "Categories/NSDictionary+Sharp.m"; sourceTree = "<group>"; };
48+
C541198316E1007B004892E9 /* NSObject+Sharp.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSObject+Sharp.h"; path = "Categories/NSObject+Sharp.h"; sourceTree = "<group>"; };
49+
C541198416E1007B004892E9 /* NSObject+Sharp.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSObject+Sharp.m"; path = "Categories/NSObject+Sharp.m"; sourceTree = "<group>"; };
4450
/* End PBXFileReference section */
4551

4652
/* Begin PBXFrameworksBuildPhase section */
@@ -118,6 +124,10 @@
118124
C541197B16DF6443004892E9 /* UIScrollView+Sharp.m */,
119125
C541197D16DF9764004892E9 /* NSArray+Sharp.h */,
120126
C541197E16DF9764004892E9 /* NSArray+Sharp.m */,
127+
C541198016E0FE97004892E9 /* NSDictionary+Sharp.h */,
128+
C541198116E0FE97004892E9 /* NSDictionary+Sharp.m */,
129+
C541198316E1007B004892E9 /* NSObject+Sharp.h */,
130+
C541198416E1007B004892E9 /* NSObject+Sharp.m */,
121131
);
122132
name = Categories;
123133
sourceTree = "<group>";
@@ -192,6 +202,8 @@
192202
C541197916DF5BB9004892E9 /* NSDate+Sharp.m in Sources */,
193203
C541197C16DF6443004892E9 /* UIScrollView+Sharp.m in Sources */,
194204
C541197F16DF9764004892E9 /* NSArray+Sharp.m in Sources */,
205+
C541198216E0FE97004892E9 /* NSDictionary+Sharp.m in Sources */,
206+
C541198516E1007B004892E9 /* NSObject+Sharp.m in Sources */,
195207
);
196208
runOnlyForDeploymentPostprocessing = 0;
197209
};
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// NSDictionary+Sharp.h
3+
// iOSLib
4+
//
5+
// Created by Sommer, Martin on 01.03.13.
6+
// Copyright (c) 2013 Sommer, Martin. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@interface NSDictionary (Sharp)
12+
13+
+(NSDictionary*)dictionaryWithObject:(NSObject*)obj;
14+
15+
@end
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// NSDictionary+Sharp.m
3+
// iOSLib
4+
//
5+
// Created by Sommer, Martin on 01.03.13.
6+
// Copyright (c) 2013 Sommer, Martin. All rights reserved.
7+
//
8+
9+
#import "NSDictionary+Sharp.h"
10+
#import "NSObject+Sharp.h"
11+
12+
@implementation NSDictionary (Sharp)
13+
14+
+(NSDictionary*)dictionaryWithObject:(NSObject*)obj
15+
{
16+
NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
17+
18+
NSDictionary *properties = [obj propertyTypeDictionary];
19+
for(NSString *key in [properties keyEnumerator])
20+
{
21+
[result setObject:[obj valueForKey:key] forKey:key];
22+
}
23+
return result;
24+
}
25+
26+
27+
28+
@end

iOSLib/Categories/NSObject+Sharp.h

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// NSObject+Sharp.h
3+
// iOSLib
4+
//
5+
// Created by Sommer, Martin on 01.03.13.
6+
// Copyright (c) 2013 Sommer, Martin. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@interface NSObject (Sharp)
12+
13+
+ (NSDictionary *)propertyTypeDictionaryOfClass:(Class)clazz;
14+
15+
- (NSDictionary *)propertyTypeDictionary;
16+
17+
@end

iOSLib/Categories/NSObject+Sharp.m

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// NSObject+Sharp.m
3+
// iOSLib
4+
//
5+
// Created by Sommer, Martin on 01.03.13.
6+
// Copyright (c) 2013 Sommer, Martin. All rights reserved.
7+
//
8+
9+
#import "NSObject+Sharp.h"
10+
#import <objc/runtime.h>
11+
12+
@implementation NSObject (Sharp)
13+
14+
/*
15+
* @returns A string describing the type of the property
16+
*/
17+
18+
+ (NSString *)propertyTypeStringOfProperty:(objc_property_t) property {
19+
const char *attr = property_getAttributes(property);
20+
NSString *const attributes = [NSString stringWithCString:attr encoding:NSUTF8StringEncoding];
21+
22+
NSRange const typeRangeStart = [attributes rangeOfString:@"T@\""]; // start of type string
23+
if (typeRangeStart.location != NSNotFound) {
24+
NSString *const typeStringWithQuote = [attributes substringFromIndex:typeRangeStart.location + typeRangeStart.length];
25+
NSRange const typeRangeEnd = [typeStringWithQuote rangeOfString:@"\""]; // end of type string
26+
if (typeRangeEnd.location != NSNotFound) {
27+
NSString *const typeString = [typeStringWithQuote substringToIndex:typeRangeEnd.location];
28+
return typeString;
29+
}
30+
}
31+
return nil;
32+
}
33+
34+
/**
35+
* @returns (NSString) Dictionary of property name --> type
36+
*/
37+
38+
+ (NSDictionary *)propertyTypeDictionaryOfClass:(Class)clazz {
39+
NSMutableDictionary *propertyMap = [NSMutableDictionary dictionary];
40+
unsigned int outCount, i;
41+
objc_property_t *properties = class_copyPropertyList(clazz, &outCount);
42+
for(i = 0; i < outCount; i++) {
43+
objc_property_t property = properties[i];
44+
const char *propName = property_getName(property);
45+
if(propName) {
46+
47+
NSString *propertyName = [NSString stringWithCString:propName encoding:NSUTF8StringEncoding];
48+
NSString *propertyType = [self propertyTypeStringOfProperty:property];
49+
[propertyMap setValue:propertyType forKey:propertyName];
50+
}
51+
}
52+
free(properties);
53+
return propertyMap;
54+
}
55+
56+
- (NSDictionary *)propertyTypeDictionary
57+
{
58+
return [NSObject propertyTypeDictionaryOfClass:[self class]];
59+
}
60+
61+
@end

0 commit comments

Comments
 (0)