在什么情况下[[NSScreen mainScreen] visibleFrame]返回null? [英] Under what circumstances would [[NSScreen mainScreen] visibleFrame] return null?
问题描述
我想要获取主屏幕的尺寸,因此我使用以下代码片段:
NSLog =%@,[[NSScreen mainScreen] visibleFrame]);
正在列印
code> mainScreen frame =(null)
这里的问题是你在Objective-C Cocoa编程中比较少的非对象之一。
visibleFrame的结果是NSRect 结构,而不是对象。要让它在NSLog行显示有意义,你必须这样做:
NSString * frameAsString = NSStringFromRect [NSScreen mainScreen] visibleFrame]);
NSLog(@mainScreen frame =%@,frameAsString);
有许多帮助函数可以将许多结构对象转换为字符串, NSStringFromPoint,NSStringFromRange等。
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?
the problem here is you're running up against one of the relatively few non-objects in Objective-C Cocoa programming.
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:
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.
这篇关于在什么情况下[[NSScreen mainScreen] visibleFrame]返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!