在什么情况下[[NSScreen mainScreen] visibleFrame]返回null? [英] Under what circumstances would [[NSScreen mainScreen] visibleFrame] return null?

查看:342
本文介绍了在什么情况下[[NSScreen mainScreen] visibleFrame]返回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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆