一个Objective-C结构的默认值以及如何测试 [英] Default value of an Objective-C struct and how to test

查看:135
本文介绍了一个Objective-C结构的默认值以及如何测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试一个属性已设置呢。我知道,与对象,我已经得到了:

I'm trying to test if a property has been set yet. I know that with objects that I've got:

CGRect ppGoalFrame;
LocalPlaySetup *localPlaySetup;

和我可以测试

if (localPlaySetup == nil)

但如果我试图用==来测试的CGRect零或== NULL

but if I attempt to test the CGRect with == nil or == NULL

if (ppGoalFrame == nil)

我得到

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

原来是的CGRect 作废,空,无...?之前,它的设置?很显然,我不能比较的CGRect一个空指针(我不能使用 ppGoalFrame ==无效);有另一种方式来测试这个? Objective-C的到目前为止是pretty很容易理解,但只要像C织机,我有点失去了。

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, I get a bit lost.

推荐答案

只有指针可以为空。的CGRect是一个结构 - 它重新presents一个连续的内存块。告诉的唯一方法,如果它已经被设置为检查其内容。

Only pointers can be null. CGRect is a struct - it represents a contiguous block of memory. The only way to tell if it has been set it to check its contents.

苹果确实提供了一个恒定的 CGRectNull 。则可以将变量设置为此,使用CGRectIsNull函数以确定它是否已被设定。 CGRectNull 是不一样的 CGRectZero 因此,如果期望的值为零,你不用担心。

Apple does provide a constant CGRectNull. You could set your variable to this and use the CGRectIsNull function to determine if it has been set. CGRectNull is not the same as CGRectZero so you need not worry if the desired value is zero.

注意CGRectNull只是包含的CGRect结构充满了苹果以后可以识别的CGRectIsNull函数值。这是不一样的null作为比较指针时。

Note that CGRectNull simply contains a CGRect struct filled with values that Apple can later identify for the CGRectIsNull function. It is not the same null as when comparing pointers.

这篇关于一个Objective-C结构的默认值以及如何测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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