-1

I am trying to store the height of the screen in NSInteger.

NSInteger *screenHeight = self.view.frame.size.height;

I also tried to store it in NSString and NSNumber, but there is always an error that says Initializing 'NSInteger' (aka 'int*') with an expression of incompatible type 'CGFloat' (aka 'float')*. I will go back to get the screen size from whatever I store it in later in the code, but what can I store a float in?

Alex3179
  • 1
  • 1

3 Answers3

2

NSInteger is typdef of basic int.

You need to use

NSInteger screenHeight = self.view.frame.size.height;

*Note: self.view.frame.size.height returns you float so why not to use float.

Also unless you really want a pointer to an integer never use NSInteger*


Edit:

In case you want NSNumber then

NSNumber *screenHeight = @(self.view.frame.size.height); //box the value to NSNumber

If you want it an NSString then

NSString *screenHeight = [NSString stringWithFormat:@"%f", self.view.frame.size.height];
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • This answer is incorrect. `NSInteger` is a typedef of `int` only on 32-bit hardware. On 64-bit, `NSInteger` is a typedef of `long`. – Rose Perrone Mar 26 '15 at 18:36
1

remove the '*' NSInteger is a primitive type.

NSInteger screenHeight = self.view.frame.size.height;
Peter Segerblom
  • 2,773
  • 1
  • 19
  • 24
0

If you need to store it as object (i.e. to add it to an array), use

NSNumber *screenHeight =[NSNumber numberWithFloat:self.view.frame.size.height];
Armand DOHM
  • 1,121
  • 1
  • 7
  • 9