我正在测试是否已经设置了属性.我知道用我拥有的物品:

CGRect ppGoalFrame;
LocalPlaySetup *localPlaySetup;

我可以测试

if (localPlaySetup == nil)

但是如果我try 用==nil或==NULL测试CGRect

if (ppGoalFrame == nil)

我明白了

invalid operands to binary == (have 'CGRect' and 'void *')

So is the CGRect "void", null, nil...? before it's set? Obviously I can't compare CGrect to a void pointer (I can't use ppGoalFrame == void); is there another way to test this? The Objective-C so far is pretty easy to understand but as soon as the C looms, 我明白了 a bit lost.

推荐答案

只有指针可以为空.CGRect是一个 struct ——它代表一个连续的内存块.唯一能判断它是否被设置为判断其内容的方法.

苹果确实提供了一个恒定的CGRectNull.您可以将变量设置为该值,并使用CGRectIsNull函数确定是否已设置该值.CGRectNullCGRectZero不一样,所以你不必担心期望值是否为零.

请注意,CGRectNull只包含一个CGRect struct ,其中填充了Apple稍后可以为CGRectIsNull函数识别的值.它与比较指针时的空值不同.

Objective-c相关问答推荐

Objective C for循环中断并继续

如何在 Cocoa 中创建字符串的 MD5 哈希?

dispatch_get_global_queue 和 dispatch_queue_create 有什么区别?

判断是否显示 UIAlertView

NSFileManager 唯一的文件名

NSString:从字符串中删除 UTF-8 重音的简单方法?

我们可以在 UIPageViewController 中自定义页面指示器吗?

如何在iphone的objective-c中以编程方式调整图像大小

何时应该使用 initWithNibName 初始化视图控制器?

Cocoa 的依赖注入框架?

如何删除应用程序可访问的所有 keys 串项目?

无法加载 xcode 项目,因为它已从另一个项目或工作区打开

当用户点击文本字段外的其他区域时如何关闭键盘?

Core Data使用原始数据类型的标量属性复选框

无法将 searchBar 设置为 firstResponder

目标覆盖 FRAMEWORK_SEARCH_PATHS 构建设置

有没有办法在不使用 UINavigationController 的情况下更改 Storyboard 中 UINavigationBar 的高度?

Xcode没有编译任何项目? 'clang 失败,退出代码 255'

在 iOS 6.0 的 Xcode 4.5 中我没有要求的日志(log)消息

CALayer:仅在一侧添加边框