Objective C convert number to NSNumber

前端 未结 5 604
情深已故
情深已故 2021-01-03 19:43

Why doesn\'t this please the compiler? Casting is supposed to work like in C as I can read here How to cast an object in Objective-C.

[p setAge:(NSNumber*)10         


        
5条回答
  •  长发绾君心
    2021-01-03 20:12

    Because NSNumber is an object and "10" in a primitive integer type, much like the difference between int and Integer in Java. You, therefore, need to call its initialiser:

    [p setAge:[NSNumber numberWithInt:10]
    

提交回复
热议问题