创建的 C 数组的 Objective-C 初始值

发布于 2024-09-05 09:57:30 字数 320 浏览 3 评论 0 原文

我创建了一个类似于经典 C 的数组(不是 NSArray 或其子数组之一) - 类似 BOOL i[5];。我想让它的所有值都等于NO。

首先,我没有找到任何有关此类数组初始值的信息(我知道在经典 C 中它们将是未定义的,但不太了解 Objective-C。我找到了有关类及其内部数据的信息[分配后,无需初始化],但不涉及简单数据类型)。

第二,如果我应该手动设置数组值 - 我应该使用 memset(...); 还是不同的东西?

为了防止可能出现的问题...我想使用此构造作为临时布尔标志数组,并且不认为它被证明可以使用类似 NSArray 的东西。

I create an array, similar to classic C (not NSArray or one of it's children) - something like BOOL i[5];. And I want to make all its values to be equal to NO.

First of all, I didn't found any information about initial values of such arrays (I know that in classic C they will be undefined, but don't know exactly about Objective-C. I found info about classes and its inner data [after allocation, without initialization], but not about simple data types).

And the second, if I should set array values manually - should I use memset(...); or something different?

To prevent possible questions... I want to use this construction as array of temporary boolean flags and don't think that it is proved to use something like NSArray here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傾城如夢未必闌珊 2024-09-12 09:57:30

如果 BOOL i[5] 是 ObjC 类的 ivar,则其内容将被初始化 (memset-ed) 为 0。

+alloc 方法

...所有其他实例变量的内存设置为 0


要在其他情况下将数组设置为全否,您可以使用

memset(i, 0, sizeof(i));

If that BOOL i[5] is an ivar of an ObjC class, its content will be initialized (memset-ed) to 0.

As described in the +alloc method:

... memory for all other instance variables is set to 0.


To set the array to all NO in other situations, you could use

memset(i, 0, sizeof(i));
楠木可依 2024-09-12 09:57:30

即使它是 0-ed 而不是像 C 中的 undef,正确的初始化也应该显式设置所有值,因为您无法知道 NO 是否为 0(它是,但假设......!),所以简单地说:

for(j = 0; j < 5; j++) i[j] = NO;

可以这样做工作。

Even if it is 0-ed instead of undef like in C, a proper initialization should set all values explicitly since you can't know if NO is 0 (it is, but hypotethically...!), so simply:

for(j = 0; j < 5; j++) i[j] = NO;

could do the job.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文