我是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.为什么它们不同?它与指针有关吗?请帮忙。
答案 0 :(得分:2)
%d是小数,你正在尝试打印浮点数。尝试
CGRect screenRect = [UIScreen mainScreen].bounds;
NSLog(@"width: %f", screenRect.size.width);
float width = [UIScreen mainScreen].bounds.size.width;
NSLog(@"width: %f", width);
答案 1 :(得分:1)
CGRect.size.width是CGFloat。如果在格式字符串中使用%f,则会得到320。
答案 2 :(得分:0)