C语言 Objective-C 结构的默认值以及如何测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2414809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:47:38  来源:igfitidea点击:

Default value of an Objective-C struct and how to test

cobjective-cstructdefault-value

提问by typeoneerror

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;

and I can test

我可以测试

if (localPlaySetup == nil)

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

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

if (ppGoalFrame == nil)

I get

我得到

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

那么CGRect“void”,null,nil ...?在设置之前?显然我不能将 CGrect 与空指针进行比较(我不能使用ppGoalFrame == void);有没有另一种方法来测试这个?到目前为止,Objective-C 很容易理解,但是一旦 C 出现,我就有点迷失了。

回答by Andrew

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.

只有指针可以为空。CGRect 是一个结构体——它代表一个连续的内存块。判断它是否已设置为检查其内容的唯一方法。

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. CGRectNullis not the same as CGRectZeroso you need not worry if the desired value is zero.

Apple 确实提供了一个常量CGRectNull. 您可以将变量设置为此,并使用 CGRectIsNull 函数来确定它是否已设置。 CGRectNull不一样,CGRectZero因此您不必担心所需的值是否为零。

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.

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

回答by gavinb

All instance variables in an Objective-C class are initialized to zero. So all pointers are nil, numbers are 0, and structs are zeroed. Since the CGRect is a plain struct, it will be initialised to origin.x=0, origin.y=0, size.width=0, size.height=0.

Objective-C 类中的所有实例变量都初始化为零。所以所有的指针都是nil,数字是 0,结构被清零。由于 CGRect 是一个普通结构,它将被初始化为origin.x=0, origin.y=0, size.width=0, size.height=0.

So to test if your CGRecthas been set, you need to compare it (by value) to zero. The CGRectIsEmptyfunction will do exactly this:

因此,要测试您CGRect是否已设置,您需要将它(按值)与零进行比较。该CGRectIsEmpty函数将执行以下操作:

if (CGRectIsEmpty(ppGoalFrame))
{
    // ...
}

回答by Martin Bissegger

From CGGeometry.h:

来自CGGeometry.h

/* Return true if `rect' is empty (that is, if it has zero width or height),
   false otherwise. A null rect is defined to be empty. */

an empty rectangle is one that has no area, where on or both sides are zero.
Use instead:

一个空矩形是一个没有面积的矩形,其中边或边为零。
改用:

CGRect newRect = CGRectNull;

回答by Carlos Ricardo

You can wrap CGRect in NSValue too:

您也可以将 CGRect 包装在 NSValue 中:

[NSValue valueWithCGRect:CGRectMake(x, y, width, height)]

[NSValue valueWithCGRect:CGRectMake(x, y, width, height)]

And then avoid the nilvalue.

然后避免该nil值。

回答by Chuck

There is no universal "nothing" value. There's nilfor objects and NULLfor pointers, but there's no equivalent for plain value types — they always have somevalue, even if it's just nonsense. You can take an arbitrary value and say "I'm going to call this the null value for this type" (say, #define IntNull 4444444and hope some calculation never accidentally returns that value), but there's no real null value.

没有普遍的“无”值。有nil对象和NULL指针,但没有普通值类型的等价物——它们总是有一些值,即使它只是无稽之谈。你可以取一个任意值并说“我将把它称为这种类型的空值”(比如,#define IntNull 4444444希望某些计算永远不会意外返回该值),但没有真正的空值。

In the case of CGRect in particular, Apple used the "pick an arbitrary value" tactic and defined CGRectNull, but CGRect variables aren't set to that by default — you have to specifically set it yourself. By default, CGRect instance variables are set to CGRectZero, but remember that this is just a normal CGRect struct at location (0,0) with 0 width and 0 height. And local CGRects in a function have a totally unpredictable value.

特别是在 CGRect 的情况下,Apple 使用了“选择任意值”策略并定义了CGRectNull,但是默认情况下 CGRect 变量没有设置为那个——你必须自己专门设置它。默认情况下,CGRect 实例变量设置为CGRectZero,但请记住,这只是位于 (0,0) 位置的普通 CGRect 结构,宽度为 0,高度为 0。并且函数中的局部 CGRects 具有完全不可预测的值。

回答by Nikita

Actually there is an obvious and elegant solution (lol):

实际上有一个明显而优雅的解决方案(笑):

SomeStruct someStruct = {0};

SomeStruct someStruct = {0};

Which just fills all the "elements" of the struct with zeroes.

它只是用零填充结构的所有“元素”。

回答by C.D. Reimer

I don't think you can do a binary comparison of a C structure, which is what CGRect is. A quick Google search and checking the C For Dummies Desk Reference didn't turn anything up. You could declare CGRect ppGoalFrame, initialize the members of the structure with default values (if they're not already), and test the structure members.

我不认为你可以对 C 结构进行二进制比较,这就是CGRect。快速谷歌搜索并检查 C For Dummies Desk Reference 没有发现任何问题。您可以声明 CGRect ppGoalFrame,使用默认值初始化结构的成员(如果它们还没有),并测试结构成员。

Update Edit: This question has been askedfor C structures.

更新编辑:此问题已被要求用于 C 结构。