0
votes

I'm using the same example on the web

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

But when I build the project I face one issue "Duplicate symbols"

duplicate symbol _OBJC_CLASS_$_OrderModel
ld: 576 duplicate symbols for architecture arm64
clang: error: linker command failed with exit code 1
1
Why don't you split the object's file in two .m and .h files? I think this is the cause of problem. If you import this header in two files it will cause a duplicate symbol error because there will be two implementations for the same class. - vbgd

1 Answers

2
votes

The @implementation should be present in a .m file and the @interface in a .h file.

You should only import the .h file. Otherwise you will have multiple implementations for the same 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

If you would want to use the ProductModel class in a View Controller for example just import the "ProductModel.h":

#import "ProductModel.h"