Created
May 3, 2015 12:22
-
-
Save royratcliffe/d232325edd2f9a3a072c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Foundation NSObject+Properties.h | |
// | |
// Copyright © 2015, Roy Ratcliffe, Pioneering Software, United Kingdom | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the “Software”), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in | |
// all copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED “AS IS,” WITHOUT WARRANTY OF ANY KIND, EITHER | |
// EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO | |
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES | |
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
// DEALINGS IN THE SOFTWARE. | |
// | |
//------------------------------------------------------------------------------ | |
@import Foundation; | |
@interface NSObject (Properties) | |
/// Uses the Objective-C run-time to obtain the object class' property names. | |
+ (NSArray *)propertyNames; | |
/// Obtains names of class properties then uses key-value coding to access the | |
/// property values by name. Answers a dictionary of property name-value | |
/// pairs. The dictionary excludes @c nil valued properties. | |
- (NSDictionary *)properties; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Foundation NSObject+Properties.m | |
// | |
// Copyright © 2015, Roy Ratcliffe, Pioneering Software, United Kingdom | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the “Software”), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in | |
// all copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED “AS IS,” WITHOUT WARRANTY OF ANY KIND, EITHER | |
// EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO | |
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES | |
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
// DEALINGS IN THE SOFTWARE. | |
// | |
//------------------------------------------------------------------------------ | |
#import "NSObject+Properties.h" | |
#import <objc/runtime.h> | |
@implementation NSObject (Properties) | |
+ (NSArray *)propertyNames | |
{ | |
unsigned int count; | |
NSMutableArray *propertyNames = [NSMutableArray array]; | |
objc_property_t *propertyList = class_copyPropertyList(self, &count); | |
if (propertyList) { | |
for (NSUInteger index = 0; index < count; index++) { | |
const char *name = property_getName(propertyList[index]); | |
[propertyNames addObject:[NSString stringWithUTF8String:name]]; | |
} | |
free(propertyList); | |
} | |
return [propertyNames copy]; | |
} | |
- (NSDictionary *)properties | |
{ | |
NSMutableDictionary *properties = [NSMutableDictionary dictionary]; | |
for (NSString *name in [[self class] propertyNames]) { | |
id value = [self valueForKey:name]; | |
if (value) { | |
properties[name] = value; | |
} | |
} | |
return [properties copy]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment