XCode编译器不会将我的变量(和指针)初始值设置为0或nil! [英] XCode compiler doesn't set my variable (and pointer) initial value to 0 or nil!

查看:330
本文介绍了XCode编译器不会将我的变量(和指针)初始值设置为0或nil!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sthis非常讨厌,因为现在我的值存储,如果他们默认含有某些(如c)。所有我的OO东西现在破碎,因为我的代表总是有时。我想知道为什么XCode这样做我,因为默认情况下XCode总是设置变量值为0或nil。

sthis is very annoying, since now my values are stored as if they contain somethin by default (like in c). All my OO stuff are now broken since my delegate are always somethin. I was wonderin why XCode do this to me, since by default XCode always set the variable value to 0 or nil.

所以如果我做
NSArray * anArray ;
然后
NSLog(%@%@,anArray);

So if i do NSArray* anArray; and then NSLog(%@"%@", anArray);

它可能崩溃或记录hasard最后分配的内存。这是非常令人沮丧的。

It could crash or log hasard last allocated memory. This is very frustrating

推荐答案

C,Objective C和C ++都将全局变量初始化为零/ null / Nil。局部变量不会自动初始化,必须显式初始化。

C, Objective C, and C++ all initialize global variables to zero/null/Nil. Local variables are not automatically initialized and must be explicitly initialized.

此外,指向NSArray的指针不是一个NSArray。在使用该指针之前,您应该安排一个NSArray实际上在它的末尾。例如,创建一个新的,更像是

Additionally, a pointer to a NSArray is not an NSArray. Before using that pointer, you should arrange for an NSArray to actually be at the end of it. For instance, make a new one, something more like


    // NSArray* anArray = new NSArray;  // if using a C++ backend
    NSArray* anArray = [[NSArray alloc] init];  // if using an Objective-C backend
    // ...
    NSLog(%@"%@", anArray);

这篇关于XCode编译器不会将我的变量(和指针)初始值设置为0或nil!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆