我是 iPhone 版 Objective C 的新手。我尝试使用以下代码来获取屏幕尺寸,但是下面的代码返回两个不同的结果:
CGRect screenRect = [UIScreen mainScreen].bounds;
NSLog(@"width: %d", screenRect.size.width);
NSUInteger width = [UIScreen mainScreen].bounds.size.width;
NSUInteger height = [UIScreen mainScreen].bounds.size.height;
NSLog(@"width: %d", width);
第一个 NSLog 输出 0,而最后一个输出 320。为什么它们不同?和指针有关系吗?请帮忙。
最佳答案
%d 表示小数,并且您正在尝试打印 float 。尝试一下
CGRect screenRect = [UIScreen mainScreen].bounds;
NSLog(@"width: %f", screenRect.size.width);
float width = [UIScreen mainScreen].bounds.size.width;
NSLog(@"width: %f", width);
关于iPhone SDK 屏幕尺寸使用相似的语法返回不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3581406/