I want to get the dimensions of the main screen, so I use this snippet:
我想获得主屏幕的尺寸,所以我使用这个片段:
NSLog(@"mainScreen frame = %@", [[NSScreen mainScreen] visibleFrame]);
It's printing
mainScreen frame = (null)
Earlier it was printing the expected dimensions of my main monitor.
之前它是打印我的主显示器的预期尺寸。
What are some possible causes of this?
这有什么可能的原因?
4 个解决方案
#1
the problem here is you're running up against one of the relatively few non-objects in Objective-C Cocoa programming.
这里的问题是你正在遇到Objective-C Cocoa编程中相对较少的非对象之一。
The result of "visibleFrame" is an NSRect structure, not an object. To get it to display meaningfully in the NSLog line, you have to do something like this:
“visibleFrame”的结果是NSRect结构,而不是对象。要使它在NSLog行中有意义地显示,您必须执行以下操作:
NSString* frameAsString = NSStringFromRect([[NSScreen mainScreen] visibleFrame]);
NSLog(@"mainScreen frame = %@", frameAsString);
There are helper functions for converting many of these structure objects to strings and back, e.g. NSStringFromPoint, NSStringFromRange, etc.
存在用于将许多这些结构对象转换为字符串并返回的辅助函数,例如, NSStringFromPoint,NSStringFromRange等
#2
The documentation on this needs to be read carefully. The "main screen", as Apple defines it, is not necessarily the screen with the menu bar. The "main screen" is the screen that is receiving keyboard events. If, for some reason the OS thinks that no screens have the keyboard focus then I could understand why mainScreen would return NULL.
有关这方面的文档需要仔细阅读。 Apple定义的“主屏幕”不一定是菜单栏的屏幕。 “主屏幕”是接收键盘事件的屏幕。如果由于某种原因操作系统认为没有屏幕具有键盘焦点,那么我就能理解为什么mainScreen会返回NULL。
To get the screen with the menu bar (And origin at (0,0)) you need to use:
要获得带有菜单栏的屏幕(并且原点为(0,0)),您需要使用:
[[NSScreen screens] objectAtIndex:0]
I've never seen this return NULL, although I won't say that it can't happen.
我从来没有见过这个返回NULL,虽然我不会说它不可能发生。
#3
-visibleFrame returns an NSRect struct, while you're using a string specifier for an object. You need to use the NSStringFromRect() function (I believe it's called) to turn the rect into a string object for NSLog().
-visibleFrame返回NSRect结构,而您正在为对象使用字符串说明符。您需要使用NSStringFromRect()函数(我相信它被调用)将rect转换为NSLog()的字符串对象。
#4
You're trying to log an object but the method isn't returning an object, it's returning a struct.
你正在尝试记录一个对象,但该方法没有返回一个对象,它返回一个结构。
Although NSStringFromRect will help you with logging, it's likely you'll want the actual integers elsewhere.
虽然NSStringFromRect可以帮助您进行日志记录,但您可能希望其他地方的实际整数。
You can accomplish both with:
您可以通过以下方式完成
NSLog(@"screen is %.2f wide and %.2f high", [[[NSScreen screens]
objectAtIndex:0] visibleFrame].size.width, [[[NSScreen screens]
objectAtIndex:0] visibleFrame].size.height);
#1
the problem here is you're running up against one of the relatively few non-objects in Objective-C Cocoa programming.
这里的问题是你正在遇到Objective-C Cocoa编程中相对较少的非对象之一。
The result of "visibleFrame" is an NSRect structure, not an object. To get it to display meaningfully in the NSLog line, you have to do something like this:
“visibleFrame”的结果是NSRect结构,而不是对象。要使它在NSLog行中有意义地显示,您必须执行以下操作:
NSString* frameAsString = NSStringFromRect([[NSScreen mainScreen] visibleFrame]);
NSLog(@"mainScreen frame = %@", frameAsString);
There are helper functions for converting many of these structure objects to strings and back, e.g. NSStringFromPoint, NSStringFromRange, etc.
存在用于将许多这些结构对象转换为字符串并返回的辅助函数,例如, NSStringFromPoint,NSStringFromRange等
#2
The documentation on this needs to be read carefully. The "main screen", as Apple defines it, is not necessarily the screen with the menu bar. The "main screen" is the screen that is receiving keyboard events. If, for some reason the OS thinks that no screens have the keyboard focus then I could understand why mainScreen would return NULL.
有关这方面的文档需要仔细阅读。 Apple定义的“主屏幕”不一定是菜单栏的屏幕。 “主屏幕”是接收键盘事件的屏幕。如果由于某种原因操作系统认为没有屏幕具有键盘焦点,那么我就能理解为什么mainScreen会返回NULL。
To get the screen with the menu bar (And origin at (0,0)) you need to use:
要获得带有菜单栏的屏幕(并且原点为(0,0)),您需要使用:
[[NSScreen screens] objectAtIndex:0]
I've never seen this return NULL, although I won't say that it can't happen.
我从来没有见过这个返回NULL,虽然我不会说它不可能发生。
#3
-visibleFrame returns an NSRect struct, while you're using a string specifier for an object. You need to use the NSStringFromRect() function (I believe it's called) to turn the rect into a string object for NSLog().
-visibleFrame返回NSRect结构,而您正在为对象使用字符串说明符。您需要使用NSStringFromRect()函数(我相信它被调用)将rect转换为NSLog()的字符串对象。
#4
You're trying to log an object but the method isn't returning an object, it's returning a struct.
你正在尝试记录一个对象,但该方法没有返回一个对象,它返回一个结构。
Although NSStringFromRect will help you with logging, it's likely you'll want the actual integers elsewhere.
虽然NSStringFromRect可以帮助您进行日志记录,但您可能希望其他地方的实际整数。
You can accomplish both with:
您可以通过以下方式完成
NSLog(@"screen is %.2f wide and %.2f high", [[[NSScreen screens]
objectAtIndex:0] visibleFrame].size.width, [[[NSScreen screens]
objectAtIndex:0] visibleFrame].size.height);