首页
/ Jastor技术文档

Jastor技术文档

2024-12-24 20:55:51作者:申梦珏Efrain

1. 安装指南

将Jastor.m和Jastor.h文件以及JastorRuntimeHelper.m和JastorRuntimeHelper.h文件复制到你的项目中。

2. 项目的使用说明

Jastor是一个Objective-C的基础类,可以通过一个字典(可能是从JSON响应中获取的)来初始化,并将字典的值赋给所有派生类的typed @properties。

Jastor支持嵌套类型、数组、NSString、NSNumber、NSDate等。

3. 项目API使用文档

  • initWithDictionary::使用字典初始化Jastor对象。
@interface Product : Jastor
@property (nonatomic, copy) NSString *name;
@property (nonatomic, retain) NSNumber *amount;
@end

@implementation Product
@synthesize name, amount;

- (void)dealloc {
	self.name = nil;
	self.amount = nil;
	
	[super dealloc];
}
@end

NSDictionary *dictionary = /* 解析JSON响应为字典 */;
Product *product = [[Product alloc] initWithDictionary:dictionary];
  • 嵌套对象:Jastor可以将嵌套对象转换为目标类型。
@interface ProductCategory : Jastor
@property (nonatomic, copy) NSString *name;
@end

@interface Product : Jastor
@property (nonatomic, copy) NSString *name;
@property (nonatomic, retain) ProductCategory *category;
@end

NSDictionary *dictionary = /* 解析JSON响应为字典 */;
Product *product = [[Product alloc] initWithDictionary:dictionary];
  • 数组:Jastor支持特定类型的数组。
@interface ProductCategory : Jastor
@property (nonatomic, copy) NSString *name;
@end

@interface Product : Jastor
@property (nonatomic, copy) NSString *name;
@property (nonatomic, retain) NSArray *categories;
@end

+ (Class)categories_class {
	return [ProductCategory class];
}
  • 自定义映射:如果从服务器接收的json字段与Objective-C的字段不同,可以在子类中重写map方法。
@interface Person : Jastor
@property (nonatomic, copy) NSString *firstName;
@property (nonatomic, copy) NSString *lastName;
@end

@implementation Person
@synthesize firstName, lastName;

- (NSDictionary *)map{
    NSMutableDictionary *map = [NSMutableDictionary dictionaryWithDictionary:[super map]];
    [map setObject:@"first_name" forKey:@"firstName"];
    [map setObject:@"last_name" forKey:@"lastName"];
    return [NSDictionary dictionaryWithDictionary:map];
}
@end

4. 项目安装方式

只需将Jastor.m和Jastor.h文件以及JastorRuntimeHelper.m和JastorRuntimeHelper.h文件复制到你的项目中即可。

登录后查看全文
热门项目推荐