Creating a singleton with allocWithZone:

巧了我就是萌 提交于 2019-11-30 09:59:08

[super alloc] will call through to allocWithZone:, which you've overridden to do something else. In order to actually get the superclass's implementation of allocWithZone: (which is what you want there) rather than the overridden version, you must send allocWithZone: explicitly.

The super keyword represents the same object as self; it just tells the method dispatch mechanism to start looking for the corresponding method in the superclass rather than the current class.

Thus, [super alloc] would go up to the superclass, and get the implementation there, which looks something like:

+ (id) alloc
{
    return [self allocWithZone:NULL];
}

Here, self still represents your custom class, and thus, your overridden allocWithZone: is run, which will send your program into an infinite loop.

From Apple's documentation:

This method exists for historical reasons; memory zones are no longer used by Objective-C.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!