Search code examples
iosobjective-cjsonmodel

jsonmodel - Model cascading (models including other models)


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

Solution

  • 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"