gpt4 book ai didi

objective-c - 原始实例变量是否在 Objective-C 中默认初始化?

转载 作者:太空狗 更新时间:2023-10-30 03:36:56 26 4
gpt4 key购买 nike

在下面的代码中,使用 _test 并期望它的值为 NO 是否安全?或者我是否需要始终在 - (id)init 中显式初始化它?

@implementation Test {
BOOL _test;
}

最佳答案

假设所有实例变量都将初始化为 0 是安全的。

然而,对于局部/方法范围的变量,情况并非如此,如果不手动初始化,它们将指向垃圾。

供将来引用,如Rob Napier points out , 这可以在 the documentation for + (id)alloc 中找到:

The isa instance variable of the new instance is initialized to a data structure that describes the class; memory for all other instance variables is set to 0.

关于objective-c - 原始实例变量是否在 Objective-C 中默认初始化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10653119/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com