- 声明时变量的默认值 -

发布于 2024-08-29 00:17:59 字数 517 浏览 7 评论 0原文

我想知道在初始化变量之前它们的默认值是什么...

例如,如果我这样做:

//myClass.h

BOOL myBOOL; // default value ?
NSArray *myArray; // default value ?
NSUInteger myInteger; // default value ?

这里还有更多例子:

//myClass.m
// myArray is not initialized, only declared in .h file

if ([myArray count] == 0) { // TRUE or FALSE ?

// do whatever

}

更一般地说,当我这样做时返回什么:

[myObjectOnlyDeclaredAndNotInitialized myCustomFunction];

谢谢您的回答。

戈蒂耶。

I was wondering what were the default values of variables before I initialized them...

For example, if I do :

//myClass.h

BOOL myBOOL; // default value ?
NSArray *myArray; // default value ?
NSUInteger myInteger; // default value ?

Some more examples here :

//myClass.m
// myArray is not initialized, only declared in .h file

if ([myArray count] == 0) { // TRUE or FALSE ?

// do whatever

}

More generally, what is returned when I do :

[myObjectOnlyDeclaredAndNotInitialized myCustomFunction];

Thank you for your answers.

Gotye.

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

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

发布评论

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

评论(3

玩物 2024-09-05 00:17:59

答案是,这取决于变量定义的范围。

Objective-C 对象的实例变量总是初始化为 0/nil/false,因为分配的内存被清零。

全局变量可能初始化为0/nil/false,因为当内存首次分配给进程时,它也会被操作系统清零。然而,当然,我从不依赖它,并且总是自己初始化它们。

局部变量未初始化,并且将包含随机数据,具体取决于堆栈的增长/收缩方式。

注意,对于指向 Objective-C 对象的指针,您可以安全地将消息发送到 nil。因此,例如:

NSArray* foo = nil;
NSLog(@"%@ count = %d", foo, [foo count]);

是完全合法的,并且运行时不会崩溃,输出如下:

2010-04-14 11:54:15.226 foo[17980:a0f] (null) count = 0

The answer is that it depends on the scope in which the variable is defined.

Instance variables of Objective-C objects are always initialised to 0/nil/false because the memory allocated is zeroed.

Global variables are probably initialised to 0/nil/false to because when memory is first allocated to a process, it is also zeroed by the operating system. However, as a matter of course, I never rely on that and always initialise them myself.

Local variables are uninitialised and will contain random data depending on how the stack has grown/shrunk.

NB for pointers to Objective-C objects, you can safely send messages to nil. So, for instance:

NSArray* foo = nil;
NSLog(@"%@ count = %d", foo, [foo count]);

is perfectly legal and will run without crashing with output something like:

2010-04-14 11:54:15.226 foo[17980:a0f] (null) count = 0
离旧人 2024-09-05 00:17:59

默认情况下 NSArray 值将为 nil ,如果你不初始化它。

by default NSArray value will be nil , if you would not initialize it.

趴在窗边数星星i 2024-09-05 00:17:59
//myClass.h

BOOL myBOOL; // default value ?
NSArray *myArray; // default value ?
NSUInteger myInteger; // default value ?

Bool myBool 如果未初始化将收到默认值 False。

NSArray *myArray 如果未分配,初始化(例如 NSArray *myArray = [[NSArray alloc] init];)将具有默认值 NIL。如果你对一个统一的数组调用 count ,你将收到一个非 0 的异常。

//myClass.m
// myArray is not initialized, only declared in .h file

if ([myArray count] == 0) { => here it should crash because of [myArray count]

// do whatever

}

对于 NSUInteger myInteger ,默认值应该是 0,文档说这是用来描述一个无符号整数(我没有;虽然这个整数不代表整数) )

当您调用 [myObjectOnlyDeclaredAndNotInitialized myCustomFunction]; 时,它应该中断,抛出异常。

希望有帮助

//myClass.h

BOOL myBOOL; // default value ?
NSArray *myArray; // default value ?
NSUInteger myInteger; // default value ?

the Bool myBool if not initialized will receive the default value of False.

NSArray *myArray if not allocated,initialized(for example NSArray *myArray = [[NSArray alloc] init];) will have the default value of NIL. if you call count on an unitialized array you will receive an exception not 0.

//myClass.m
// myArray is not initialized, only declared in .h file

if ([myArray count] == 0) { => here it should crash because of [myArray count]

// do whatever

}

for the NSUInteger myInteger the default value should be 0, the documentation says that this is used to describe an unsigned integer(i didn;t represent integers with this one though)

when you call [myObjectOnlyDeclaredAndNotInitialized myCustomFunction]; it should break, throw an exception.

hope it helps

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