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

时间:2016-07-27 10:38:08

标签: ios objective-c jsonmodel

我在网上使用相同的例子

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

但是当我构建项目时,我面临一个问题&#34;重复符号&#34;

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

1 个答案:

答案 0 :(得分:2)

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

您应该只导入.h文件。否则,您将为同一个类实现多个实现。

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

如果您想在View Controller中使用ProductModel类,只需导入“ProductModel.h”:

#import "ProductModel.h"