2

I am using JSONModle, but i meet a problem, i am not sure how can i handle it?

@interface MessageModel : JSONModel

@property(nonatomic, strong) NSString*           create_at;
@property(nonatomic, assign) long                message_id;
@property(nonatomic, assign) int                 message_type;    
@property(nonatomic, strong) NSString<Optional>* text;
@property(nonatomic, assign) int<Optional>       background_no;   
@end

About property background_no, it's type is int, can i use Optional? If not, how can i do?

BlackMamba
  • 10,054
  • 7
  • 44
  • 67
  • This questions has been asked literally hundreds of times on public repos and so, just use the delegate method propertyIsOptional: – Marin Todorov Apr 28 '14 at 07:46

1 Answers1

4

You can only apply protocols to Objective-C objects, not to primitive types. You will therefore need to use NSNumber to store the int:

@property(nonatomic) NSNumber<Optional> *background_no;

and use auto-boxing, to store it:

someObject.background_no = @(123);
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • 1
    http://stackoverflow.com/questions/21778271/how-to-make-primitive-type-properties-optional what's about this method? – BlackMamba Apr 28 '14 at 07:01
  • @BlackMamba That isn't really the question you asked. That other method may work, I've no idea. I would suggest searching for and trying other solutions *before* asking questions. – trojanfoe Apr 28 '14 at 07:03
  • @BlackMamba: Yes, the **propertyIsOptional** method works just fine, I'm using it in my iOS App for several primitive properties. For objects I'm still applying the **Optional** protocol. – Thomas C. G. de Vilhena Jun 06 '14 at 16:16