views:

12877

answers:

3

Total newbie question but this is driving me mad! I try myInt = [myFloat integerValue]; and I get an error saying essentially integerValue doesn't work on floats. How do I do it?

+5  A: 

what's wrong with:

int myInt = myFloat;

bear in mind this'll use the default rounding rule, which is towards zero (i.e. -3.9f becomes -3)

Alnitak
+10  A: 

I'm pretty sure C-style casting syntax works in Objective C, so try that, too:

int myInt = (int) myFloat;

It might silence a compiler warning, at least.

unwind
A: 

In support of unwind, remember that Objective-C is a superset of C, rather than a completely new language.

Anything you can do in regular old ANSI C can be done in Objective-C.

Matthew Schinckel