OSReflectionKit
OSReflectionKit 是一个轻量级对象反射库,用于 iOS 和 Mac OS X,它允许您从简单的 NSDictionary 对象或 JSON 字符串中实例化对象。例如,下面是如何从字典或甚至直接从 JSON 字符串中实例化对象的示例
从 JSON 字符串加载数据
NSString *jsonString = nil;
// ... Obtain the jsonString data
jsonString = @"{\"name\" : \"Champions\", \"imageURL\" : \"http://www.cruzeiro.com.br/imagem/imgs/escudo.png\"}"; // Sample
// ...
// Instantiate the Category object with the content from the JSON string
Category *category = [Category objectFromJSON:jsonString];
版本 0.7 现在支持在序列化/反序列化过程中自动将 NSSet
属性转换为 NSArray
并将它们序列化/反序列化为 JSON 字符串。
从字典加载数据
NSDictionary *categoryDict = nil;
// ... Obtain the dictionary data
categoryDict = @{@"name" : @"Champions", @"imageURL" : @"http://www.cruzeiro.com.br/imagem/imgs/escudo.png"}; // Sample
// ...
// Instantiate the Category object with the content from the dictionary
Category *category = [Category objectFromDictionary:categoryDict];
核心数据
OSReflectionKit 还支持 NSManagedObject 对象。
如果您想试试看,可以下载项目并打开 OSReflectionKit+CoreDataExample 项目 :)
如何安装
- CocoaPods 是添加 OSReflectionKit 或 OSReflectionKit+CoreData 到项目中的推荐方式。
- 如果您愿意,可以直接 下载 OSReflectionKit 并将 OSReflectionKit 文件夹中的文件添加到项目中。
要求
OSReflectionKit
- iOS 5.0 或更高版本。
- Mac OS X 10.7 或更高版本。
- ARC。
OSReflectionKit+CoreData
OSReflectionKit
- iOS 5.0 或更高版本。
- ARC。
Cocoapods
OSReflectionKit
- 在你的 Podfile 中添加 OSReflectionKit 的依赖项
pod 'OSReflectionKit', '~> 0.7'
- 运行
pod install
安装 pod。 - 使用
#import "NSObject+OSReflectionKit.h"
导入 OSReflectionKit 到任何需要它的地方。
OSReflectionKit+CoreData
- 在你的 Podfile 中添加 OSReflectionKit+CoreData 的依赖项
pod 'OSReflectionKit+CoreData', '~> 0.7'
- 运行
pod install
安装 pod。 - 使用
#import "NSManagedObject+OSReflectionKit.h"
导入 OSReflectionKit+CoreData 到任何需要它的地方。
很简单。
示例用法
下载项目,打开 OSReflectionKitExample 项目并尝试一些代码 :)
还有用于示例 OSReflectionKit+CoreData 的项目。
非 ARC 使用
- 库文件基于 ARC,如果你想在非 ARC 项目中使用它,请给库文件添加编译器标志
-fobjc-arc
。
在 Xcode 中设置编译器标志,请转到你的活动目标,选择“构建设置”标签。现在选择所有 OSReflectionKit 源文件,按 Enter,输入 -fobjc-arc
并点击“完成”以启用 OSReflectionKit 的 ARC。
示例
自定义类
假设您有一个简单的类,如下所示
#import <Foundation/Foundation.h>
@interface Category : NSObject
@property (nonatomic, strong) NSNumber *categoryId;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSURL *imageURL;
@end
要启用此类对象的反射,只需将 NSObject+OSReflectionKit.h
文件导入到您要调用反射方法的地方。您还可以将 NSObject+OSReflectionKit.h
文件导入项目的 prefix.pch
文件中,使其在整个项目中可用。
#import "NSObject+OSReflectionKit.h"
// ...
categoryDict = @{@"categoryId" : @(1),
@"name" : @"Champions",
@"imageURL" : @"http://www.cruzeiro.com.br/imagem/imgs/escudo.png"}; // Sample dictionary
// Instantiate the Category object with the content from the dictionary
Category *category = [Category objectFromDictionary:categoryDict];
NSLog(@"Category description: %@", [category fullDescription]);
库将自动将字典键与 Category
类的属性名称匹配。如果您在字典中有不同的键,如下所示
categoryDict = @{@"id" : @(1),
@"name" : @"Champions",
@"image" : @"http://www.cruzeiro.com.br/imagem/imgs/escudo.png"};
您可以在 Category
类中实现一个自定义映射方法,将每个不同的键转换为目标属性
#import "Category.h"
@implementation Category
+ (NSDictionary *)reflectionMapping
{
return @{@"id":@"categoryId", @"image":@"imageURL,*"};
}
- (void)reflectionTranformsValue:(id)value forKey:(NSString *)propertyName
{
if([propertyName isEqualToString:@"imageURL"])
{
NSString *imageURLString = value;
if(imageURLString)
self.imageURL = [NSURL URLWithString:imageURLString];
}
}
@end
反射映射字典可能包含一个自定义类 @"customObject,<CustomClass>"
或包含一个*
的映射字符串,如上面的 imageURL 属性。
许可证
版权所有 © 2013 iAOS 软件。保留所有权利。
特此免费许可,任何获取本软件及其相关文档文件(以下简称“软件”)副本的个人均可免费使用本软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本,并允许将软件提供给他人,前提是遵守以下条件
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
如果您在酒吧遇到版权持有人,您有义务为其买一杯啤酒。
本软件按“原样”提供,不提供任何形式的质量保证,无论是明示的、默示的,包括但不限于适销性、特定用途适用性和非侵异物。