Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I cast an integer to an id in iOS

My code is something like this:

[request setPostValue:employee.empId forKey:@"empId"];

where employee is a "POCO" object that has a property empId which is int, but the method is waiting an identifier.

I've tried already sending setPostValue:&employee.empId and setPostValue:[employee.empId] but still getting errors.

Can I get the (id) in any way? or must I create another object from the property?

like image 637
Andresps2 Avatar asked Jul 29 '11 22:07

Andresps2


2 Answers

Yeah, you need to create another object from the integer before you can hand it off to a method that’s expecting an id.

[request setPostValue:[NSNumber numberWithInt:employee.empId] forKey:@"empId"];

Alternatively, with more recent versions of the SDK (post-iOS 6, I think?), you can use Objective-C’s new boxing literals:

[request setPostValue:@(employee.empId) forKey:@"empId"];
like image 105
Noah Witherspoon Avatar answered Oct 10 '22 22:10

Noah Witherspoon


[request setPostValue:[NSNumber numberWithInt:employee.empId] forKey:@"empId"];
like image 42
Andrea Bergia Avatar answered Oct 10 '22 22:10

Andrea Bergia