JYModel 1.1.0

JYModel 1.1.0

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最新版本2018年4月

XJYao 维护。



JYModel 1.1.0

  • XuJiayao

English Introduction

JYModel

适用于 iOS 的模型框架。

安装

Cocoapods

pod 'JYModel'

手动

  1. 下载所有文件。
  2. 将源文件添加到您的 Xcode 项目中。
  3. 导入 "JYModel/JYModel.h"

使用

NSObject+JYModelGeneration

您可以使用它自动从JSON生成属性代码,并自动将代码写入模拟器的头文件。 (如果您想自动写入,请使用模拟器!)

让我们看看如何使用它。

这里有一个JSON:{ "data" : { "person" : { "name" : "Tom", "age" : 21, "gender" : 1, "isStudent" : true, "height" : 180.3, "id" : 14124897432759830, "school" : { "schoolName" : "what?", "city" : "Beijing" } } } }

  1. 让我们分析这个JSON。我需要一个用于"person"的'Person'类,需要一个用于"school"的'School'类。

所以我创建了两个模型:Person.h|m, School.h|m。

  1. 但是我发现“person”中的JSON是有价值的,所以我需要在Person.m中实现'method 'startKeyPathFromJSONToGenerateProperties''方法,并使用'->'来让其知道键路径。如果您不实现此方法或返回nil,它将使用完整的JSON。
+ (NSString *)startKeyPathFromJSONToGenerateProperties {
    return @"data->person";
}
  1. 我需要告诉它'Person.h'的路径。您可以在plist中添加一行,将键设置为"ProjectPath",将值设置为"$(SRCROOT)/$(PROJECT_NAME)"。然后实现Person.m中的'classHeadFilePath'方法来让其知道它的位置。当然,如果您不使用模拟器或不想自动写入,请忽略此步骤。
+ (NSString *)classHeadFilePath {
    NSString *infoPlistPath = [[NSBundle mainBundle]pathForResource:@"Info.plist" ofType:nil];
    NSDictionary *infoDict = [NSDictionary dictionaryWithContentsOfFile:infoPlistPath];
    NSString *projectPath = [infoDict objectForKey:@"ProjectPath"];
    NSString *filePath = [NSString stringWithFormat:@"%@/%@.h", projectPath, NSStringFromClass([self class])];
    return filePath;
}
  1. "id"是iOS系统的关键字。我们不能将其用作属性名。我需要用一个其他名字来替代它,例如"identifier"。所以在Person.m中实现'customPropertyNameForKeyMapper'方法,并返回一个带有NSDictionary的映射器(键是JSON中的名字,值是您想要的名称)。
+ (NSDictionary *)customPropertyNameForKeyMapper {
    return @{@"id" : @"identifier"};
}
  1. 我需要让它知道我想使用'School'类来表示"school"。在Person.m中实现'customClassForKeyMapper'方法,返回一个带有NSDictionary的映射器(键是名字,值是类名)。它支持枚举。
+ (NSDictionary *)customClassForKeyMapper {
    return @{@"school" : @"School"};
}
  1. 如果您想自己复制粘贴属性,没问题,在Person.m中实现'shouldAutoWritingProperties'方法,并返回NO,它不会自动将属性写入头文件。当然,如果您不使用模拟器,您必须自己复制粘贴。
+ (BOOL)shouldAutoWritingProperties {
    return NO;
}
  1. 在School.m中,如果需要,重复上述步骤。

  2. 最后,根据您的JSON数据类型,调用'autoGeneratePropertiesWithJSONString'、'autoGeneratePropertiesWithJSONDict'或'autoGeneratePropertiesWithJSONData'。它将返回结果。如果结果为nil,表示有问题,您可以在Xcode控制台中查看日志。

NSString *result = [Person autoGeneratePropertiesWithJSONString:json];
NSLog(@"%@", result);

打印

@property (nonatomic, assign) NSInteger gender;

@property (nonatomic, assign) double height;

@property (nonatomic, strong) School *school;

@property (nonatomic, assign) NSInteger identifier;

@property (nonatomic, assign) NSInteger age;

@property (nonatomic, assign) BOOL isStudent;

@property (nonatomic, copy) NSString *name;

  1. 您必须自己导入或@class您的自定义类。

中文介绍

JYModel

适用于iOS的model框架

安装说明

Cocoapods

pod 'JYModel'

手动安装

pod 'JYModel'

  1. 下载所有文件
  2. 将源文件添加到工程中
  3. 在需要的地方导入 "JYModel/JYModel.h"

用法

NSObject+JYModelGeneration

NSObject+JYModelGeneration是一个可以根据JSON自动生成属性声明代码的工具,且在模拟器上支持自动写入到头文件。如果你在真机上运行,必须手动复制工具生成的字符串,然后粘贴到头文件中。

接下来让我们看看如何使用它。

这是一个JSON示例: { "data" : { "person" : { "name" : "Tom", "age" : 21, "gender" : 1, "isStudent" : true, "height" : 180.3, "id" : 14124897432759830, "school" : { "schoolName" : "what?", "city" : "Beijing" } } } }

  1. 我们来分析一下这个JSON。我们需要一个'Person'类来对应"person"字段,一个'School'类来对应"school"字段。因此,这里创建了两个model:Person.h|m, School.h|m。

  2. 但是,分析后发现,有价值的JSON是从"person"字段开始的,所以需要在Person.m里实现方法'startKeyPathFromJSONToGenerateProperties'并使用'-'连接关键字形成路径,通过这个方法返回。如果你没有实现这个方法,或返回nil,则默认使用完整的JSON。

+ (NSString *)startKeyPathFromJSONToGenerateProperties {
    return @"data->person";
}
  1. 如果要自动写入,必须告诉它Person.h的本地绝对路径。你可以在plist中添加一行,key设置为"ProjectPath",value设置为"$(SRCROOT)/$(PROJECT_NAME)"。然后实现方法'classHeadFilePath'返回文件路径。当然,如果你使用的是真机,或者你不希望自动写入,可以忽略这一步。
+ (NSString *)classHeadFilePath {
    NSString *infoPlistPath = [[NSBundle mainBundle]pathForResource:@"Info.plist" ofType:nil];
    NSDictionary *infoDict = [NSDictionary dictionaryWithContentsOfFile:infoPlistPath];
    NSString *projectPath = [infoDict objectForKey:@"ProjectPath"];
    NSString *filePath = [NSString stringWithFormat:@"%@/%@.h", projectPath, NSStringFromClass([self class])];
    return filePath;
}
  1. "id"在iOS中是一个关键字,我们不能用它作为属性名,需要用其他名字来代替它,例如"identifier"。在Person.m中实现'customPropertyNameForKeyMapper'然后return映射表(Key是JSON中的字段名,value是你自定义的新名字)。
+ (NSDictionary *)customPropertyNameForKeyMapper {
    return @{@"id" : @"identifier"};
}
  1. 如果你需要为"school"字段自定义为'School'类,在Person.m里实现'customClassForKeyMapper',return映射表(key是JSON中的字段名,value是类名)。支持枚举。
+ (NSDictionary *)customClassForKeyMapper {
    return @{@"school" : @"School"};
}
  1. 如果你希望自己手动复制并粘贴最终生成的结果,在Person.m中实现'shouldAutoWritingProperties',return NO,它将不会自动写入到头文件中。当然,如果你使用的是真机,则必须手动复制粘贴。
+ (BOOL)shouldAutoWritingProperties {
    return NO;
}
  1. 在School.m中,如果需要的话,重复上述步骤。

  2. 最后,根据您JSON数据的类型,选择调用 'autoGeneratePropertiesWithJSONString'、'autoGeneratePropertiesWithJSONDict' 或 'autoGeneratePropertiesWithJSONData'。将会返回最终生成的结果,如果返回nil,则某个地方发生了错误,具体原因可以在Xcode的控制台查看LOG。

NSString *result = [Person autoGeneratePropertiesWithJSONString:json];
NSLog(@"%@", result);

打印结果

@property (nonatomic, assign) NSInteger gender;

@property (nonatomic, assign) double height;

@property (nonatomic, strong) School *school;

@property (nonatomic, assign) NSInteger identifier;

@property (nonatomic, assign) NSInteger age;

@property (nonatomic, assign) BOOL isStudent;

@property (nonatomic, copy) NSString *name;

  1. 您必须手动导入自定义类的头文件或者 @class 类。