iPhone SDK 屏幕尺寸使用相似的语法返回不同的结果

标签 iphone objective-c

我是 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/

相关文章:

iphone - 什么是 UITextView 的 "purpose description"?

objective-c - 在 NSTable 中获取 NSButton 的行

objective-c - CI高斯梯度失效

iphone - (iphone) UIImageView 设置图像 : leaks?

带有 SSL 客户端证书的 iPhone 应用程序

iphone - iPhone 上的 98kHz 音频文件

iOS:LocationManager 更新,什么时候停止更新?

ios - applicationDidEnterBackground 方法中的 backgroundTimeRemaining 值不正确

ios - UITableView 在滚动时崩溃(内存警告)

iphone - 带有 nib 的 ViewController 的子类