jsonmodel - 模型级联(模型包括其他模型)

jsonmodel - Model cascading (models including other models)

我在网络上使用相同的示例

OrderModel.h

@protocol ProductModel
@end

@interface ProductModel : JSONModel
@property (assign, nonatomic) int id;
@property (strong, nonatomic) NSString* name;
@property (assign, nonatomic) float price;
@end

@implementation ProductModel
@end

@interface OrderModel : JSONModel
@property (assign, nonatomic) int order_id;
@property (assign, nonatomic) float total_price;
@property (strong, nonatomic) NSArray<ProductModel>* products;
@end

@implementation OrderModel
@end

但是当我构建项目时我遇到了一个问题"Duplicate symbols"

duplicate symbol _OBJC_CLASS_$_OrderModel
ld: 576 duplicate symbols for architecture arm64
clang: error: linker command failed with exit code 1

@implementation 应出现在 .m 文件中,@interface 应出现在 .h 文件中。

您应该只导入 .h 文件。否则,您将对同一个 class.

有多个实现

ProductModel.h:

@interface ProductModel : JSONModel
@property (assign, nonatomic) int id;
@property (strong, nonatomic) NSString* name;
@property (assign, nonatomic) float price;
@end

ProductModel.m:

#import "ProductModel.h"    

@implementation ProductModel
@end

OrderModel.h:

@interface OrderModel : JSONModel
@property (assign, nonatomic) int order_id;
@property (assign, nonatomic) float total_price;
@property (strong, nonatomic) NSArray<ProductModel>* products;
@end

OrderModel.m

#import "OrderModel.h"

@implementation OrderModel
@end

例如,如果您想在视图控制器中使用 ProductModel class,只需导入 "ProductModel.h":

#import "ProductModel.h"