简体   繁体   English

试图在NSInteger中存储浮点数(或了解如何将其存储在其他东西中)

[英]Trying to store float within NSInteger (or find out how to store it in something else)

I am trying to store the height of the screen in NSInteger . 我正在尝试将屏幕的高度存储在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')*. 我也尝试将其存储在NSStringNSNumber中 ,但是始终会出现一个错误,提示使用不兼容类型'CGFloat'(aka'float')*的表达式初始化'NSInteger '( aka'int *')。 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? 我将回去从稍后在代码中存储的内容中获取屏幕尺寸,但是我可以在其中存储浮点数呢?

NSInteger is typdef of basic int . NSInteger是基本int typdef。

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. * 注意:self.view.frame.size.height返回float ,为什么不使用浮点数。

Also unless you really want a pointer to an integer never use NSInteger* 另外,除非您真的想要一个指向整数的指针,否则不要使用NSInteger*


Edit: 编辑:

In case you want NSNumber then 如果您想要NSNumber

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

If you want it an NSString then 如果你想要一个NSString然后

NSString *screenHeight = [NSString stringWithFormat:@"%f", self.view.frame.size.height];

删除'*'NSInteger是原始类型。

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

如果您需要将其存储为对象(即将其添加到数组),请使用

NSNumber *screenHeight =[NSNumber numberWithFloat:self.view.frame.size.height];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM