我发现Objective-C/cocoa框架的一个例子有以下代码.
int main()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Create an array
NSArray *month = [NSArray arrayWithObjects:@ ... nill];
[pool drain];
}
Run Code Online (Sandbox Code Playgroud)
Q1:这背后的魔力是什么(为什么我需要NSAutoreleasePool代码?)?NSAutoreleasePool和pool drain块之间发生了什么魔力?我觉得我不需要释放*month自己.这是因为它在NSAutoreleasePool和pool drain块内吗?
Q2:使用Xcode,我已经获得了main()函数.在这种情况下,我该如何使用NSAutoreleasePool和pool drain?
例如 :
int main(int argc, char *argv[])
{
//NSAutoreleasePool *pool = [[[NSAutoreleasePool] alloc] init];
return NSApplicationMain(argc, (const char **) argv);
}
Run Code Online (Sandbox Code Playgroud)