- 声明时变量的默认值 -
我想知道在初始化变量之前它们的默认值是什么...
例如,如果我这样做:
//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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
答案是,这取决于变量定义的范围。
Objective-C 对象的实例变量总是初始化为 0/nil/false,因为分配的内存被清零。
全局变量可能初始化为0/nil/false,因为当内存首次分配给进程时,它也会被操作系统清零。然而,当然,我从不依赖它,并且总是自己初始化它们。
局部变量未初始化,并且将包含随机数据,具体取决于堆栈的增长/收缩方式。
注意,对于指向 Objective-C 对象的指针,您可以安全地将消息发送到 nil。因此,例如:
是完全合法的,并且运行时不会崩溃,输出如下:
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:
is perfectly legal and will run without crashing with output something like:
默认情况下 NSArray 值将为 nil ,如果你不初始化它。
by default NSArray value will be nil , if you would not initialize it.
Bool myBool 如果未初始化将收到默认值 False。
NSArray *myArray 如果未分配,初始化(例如 NSArray *myArray = [[NSArray alloc] init];)将具有默认值 NIL。如果你对一个统一的数组调用 count ,你将收到一个非 0 的异常。
对于 NSUInteger myInteger ,默认值应该是 0,文档说这是用来描述一个无符号整数(我没有;虽然这个整数不代表整数) )
当您调用
[myObjectOnlyDeclaredAndNotInitialized myCustomFunction];
时,它应该中断,抛出异常。希望有帮助
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.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