Re: Getting class when instantiating a singleton


Steve Christensen
 

On Aug 30, 2017, at 2:44 PM, Alex Zavatone <zav@...> wrote:

On Aug 30, 2017, at 4:40 PM, Steve Christensen <punster@...> wrote:

That's "instance = [self new];", right? :)

Yes.  

I’m trying this out to see if this is valid.  

From the headers for NSObject.h;
+ (instancetype)new OBJC_SWIFT_UNAVAILABLE("use object initializers instead");

Sure, why not, at least for Obj-C? The method description says, "Allocates a new instance of the receiving class, sends it an init message, and returns the initialized object."

And I checked out NSObject.mm at <https://opensource.apple.com/source/objc4/objc4-532/runtime/NSObject.mm.auto.html> and the method implementation looks like this...

+ (id)new {
    return [[self alloc] init];
}

...which sure looks a lot like the description.

Join cocoa@apple-dev.groups.io to automatically receive all group messages.