0

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
Oceanic
  • 1,418
  • 2
  • 12
  • 19
Ricardo
  • 7,921
  • 14
  • 64
  • 111
  • 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 Jul 27 '16 at 10:53

1 Answers1

2

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"
vbgd
  • 1,937
  • 1
  • 13
  • 18
  • According to docs ProductModel should be a protocol, isn't it? – Ricardo Jul 27 '16 at 11:17
  • From what I am understanding from JSONModel documentation, you need @protocol ProductModel for using: @ property (strong, nonatomic) NSArray* products;. You can define the protocol in ProductModel.h and everything will be fine. – vbgd Jul 27 '16 at 11:27