Ale*_*one 1 primitive initialization objective-c
我是否需要显式归零基元,即设置为BOOL
s NO
,将int
s 设置为0
?
我需要明确地分配NSString*
到nil
或@""
?
我知道指针必须明确设置nil
,否则它们可能会被垃圾填满.(或者仅适用于Objective-C++?)
这取决于你所谈论的变量类型.Globals,静态变量和实例变量已经保证初始化为0.
局部变量是一个不同的故事.他们从来没有初始化所有预设选项,直到您初始化你不应该阅读它们的值或设置.没有必要将它们具体地初始化为0.例如,以下代码非常冗余:
Controller *controller = nil;
int countOfThings = 0;
controller = [Controller sharedInstance];
countOfThings = controller.totalThings - controller.thingsUsed;
Run Code Online (Sandbox Code Playgroud)
相反,您应该将变量初始化为您实际需要的值:
Controller *controller = [Controller sharedInstance];
int countOfThings = controller.totalThings - controller.thingsUsed;
Run Code Online (Sandbox Code Playgroud)