iosjsonmodel

iOS JSONModel property which is correct


if Data is

{ "id": "10", "country": "Germany", "dialCode": 49, "isInEurope": true }

someone using

@interface CountryModel : JSONModel
@property (assign, nonatomic) int id;
@property (strong, nonatomic) NSString* country;
@property (strong, nonatomic) NSString* dialCode;
@property (assign, nonatomic) BOOL isInEurope;
@end

other using

@interface CountryModel : JSONModel
@property (nonatomic) int id;
@property (nonatomic) NSString* country;
@property (nonatomic) NSString* dialCode;
@property (nonatomic) BOOL isInEurope;
@end

Which is better usage?


Solution

  • Both the methods are correct. Declaration of properties depends on your requirement.Properties are used to declare a class’s accessor methods. How can a class access model's data.While declaring property you can then optionally provide set of property attributes that define the storage semantics and other behaviors of the property.When we are defining an object's property as weak/strong we are defining its accessibility to the class.

    It depends on how you need to access the data. If you want you object to thread safe,you can define as nonatomic. By defining it as strong/ assign it defines that you own the object.And by defining it as weak you dont own your object. Check this link for more info.

    Hope it helps. Happy Coding!!