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

问题描述

这很烦人,因为现在我的值被存储为好像它们默认包含某些内容(例如在C中).我的所有OO东西现在都坏了,因为我的代表始终都是.我不知道为什么Xcode会对我这样做,因为默认情况下Xcode始终将变量值设置为0或nil.

this is very annoying, since now my values are stored as if they contain something by default (like in C). All my OO stuff are now broken since my delegate are always something. 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 ++都将全局变量初始化为零/空/无.局部变量不会自动初始化,必须显式初始化.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 14:50