Getting class when instantiating a singleton


Alex Zavatone
 

I did this a few years ago but can’t find my sample and figured I’d ask the brain trust here.

In iOS, when I’m making an Objective-C singleton, in the public method that returns the instanceType, we need to put the class name.  Is there a way to do this dynamically instead of entering the class name all the time?  

For example, where I have “DataSingleton" below, can’t we get the name of class instance without having to enter the class name?  This would be so much nicer and time saving.

@implementation CrewActionsDataSingleton

+ (instancetype)sharedInstance
{
   static DataSingleton *instance;
   static dispatch_once_t onceToken;

   dispatch_once(&onceToken, ^{
       instance = [[self alloc] init];
   });
   return instance;
}



Thanks in advance.

Alex Zavatone


Jon Gotow
 

Couldn't you just make it:

static id instance;

instead?

- Jon

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

I did this a few years ago but can’t find my sample and figured I’d ask the brain trust here.

In iOS, when I’m making an Objective-C singleton, in the public method that returns the instanceType, we need to put the class name. Is there a way to do this dynamically instead of entering the class name all the time?

For example, where I have “DataSingleton" below, can’t we get the name of class instance without having to enter the class name? This would be so much nicer and time saving.


Alex Zavatone
 

I wish.  

Pointer to non-const type ‘id’ with no explicit ownership.


On Aug 30, 2017, at 3:40 PM, Jon Gotow <gotow@...> wrote:

static id instance;


Jon Gotow
 

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

I wish.

Pointer to non-const type ‘id’ with no explicit ownership.
No no - not:

static id *instance;

but

static id instance;

The id type is itself a pointer to an object.

- Jon


Alex Zavatone
 

Awesome. TY. +1

On Aug 30, 2017, at 3:49 PM, Jon Gotow <gotow@...> wrote:


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

I wish.

Pointer to non-const type ‘id’ with no explicit ownership.
No no - not:

static id *instance;

but

static id instance;

The id type is itself a pointer to an object.

- Jon


Ben Kennedy
 

On 30 Aug 2017, at 1:37 pm, Alex Zavatone <zav@...> wrote:

In iOS, when I’m making an Objective-C singleton, in the public method that returns the instanceType, we need to put the class name. Is there a way to do this dynamically instead of entering the class name all the time?
Why is this necessary? Apologies if I'm missing something obvious, but what's wrong with literally using DataSingleton (in your example)?

Even if the method is called on a subclass it should still work as desired because you're calling [self alloc], not [DataSingleton alloc].

-ben


Alex Zavatone
 

On Aug 30, 2017, at 3:54 PM, Ben Kennedy <ben@...> wrote:


On 30 Aug 2017, at 1:37 pm, Alex Zavatone <zav@...> wrote:

In iOS, when I’m making an Objective-C singleton, in the public method that returns the instanceType, we need to put the class name. Is there a way to do this dynamically instead of entering the class name all the time?
Why is this necessary? Apologies if I'm missing something obvious, but what's wrong with literally using DataSingleton (in your example)?

Even if the method is called on a subclass it should still work as desired because you're calling [self alloc], not [DataSingleton alloc].

-ben
It makes the code copy/paste-able. No dependency on the name of class you are adding it to.


Ben Kennedy
 

On 30 Aug 2017, at 1:55 pm, Alex Zavatone <zav@...> wrote:

Why is this necessary? Apologies if I'm missing something obvious, but what's wrong with literally using DataSingleton (in your example)?
It makes the code copy/paste-able. No dependency on the name of class you are adding it to.
If saving developer effort is your goal, how about setting up a text snippet in Xcode, so that you can avoid the penalty of needlessly un-typed code?

(By "un-typed" mean in the language sense, not the keyboard sense; pun unintended.)

-ben


Alex Zavatone
 


On Aug 30, 2017, at 3:54 PM, Ben Kennedy <ben@...> wrote:


On 30 Aug 2017, at 1:37 pm, Alex Zavatone <zav@...> wrote:

In iOS, when I’m making an Objective-C singleton, in the public method that returns the instanceType, we need to put the class name.  Is there a way to do this dynamically instead of entering the class name all the time?  

Why is this necessary? Apologies if I'm missing something obvious, but what's wrong with literally using DataSingleton (in your example)?

Even if the method is called on a subclass it should still work as desired because you're calling [self alloc], not [DataSingleton alloc].

-ben




I’m able to turn the sharedInstance method from being dependent on my changing the class name and being this:

@implementation CrewActionsDataSingleton

+ (instancetype)sharedInstance
{
    static TheClassName *instance;
    static dispatch_once_t onceToken;

    

    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

 To this:

@implementation CrewActionsDataSingleton

+ (instancetype)sharedInstance
{
    static id instance; 
    static dispatch_once_t onceToken;

    

    dispatch_once(&onceToken, ^{
        [self new];
    });
    return instance;
}

I’m able to turn the sharedInstance method from being dependent on my changing the class name and being this: Good point on making a snippet.  I think I change computers too often though. It’s another thing to manage.

Thanks for the tips.  Best brain trust ever.

Alex Zavatone


Steve Christensen <punster@...>
 

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


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


On Aug 30, 2017, at 3:54 PM, Ben Kennedy <ben@...> wrote:


On 30 Aug 2017, at 1:37 pm, Alex Zavatone <zav@...> wrote:

In iOS, when I’m making an Objective-C singleton, in the public method that returns the instanceType, we need to put the class name.  Is there a way to do this dynamically instead of entering the class name all the time?  

Why is this necessary? Apologies if I'm missing something obvious, but what's wrong with literally using DataSingleton (in your example)?

Even if the method is called on a subclass it should still work as desired because you're calling [self alloc], not [DataSingleton alloc].

-ben




I’m able to turn the sharedInstance method from being dependent on my changing the class name and being this:

@implementation CrewActionsDataSingleton

+ (instancetype)sharedInstance
{
    static TheClassName *instance;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

 To this:

@implementation CrewActionsDataSingleton

+ (instancetype)sharedInstance
{
    static id instance; 
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        [self new];
    });
    return instance;
}

I’m able to turn the sharedInstance method from being dependent on my changing the class name and being this: Good point on making a snippet.  I think I change computers too often though. It’s another thing to manage.

Thanks for the tips.  Best brain trust ever.

Alex Zavatone



Alex Zavatone
 


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");

From the Quick Help ins[pector panel.

+ (instancetype)new;
Description

Allocates a new instance of the receiving class, sends it an initmessage, and returns the initialized object.

This method is a combination of alloc and init. Like alloc, it initializes the isa instance variable of the new object so it points to the class data structure. It then invokes the init method to complete the initialization process.
Returns
A new instance of the receiver.
AvailabilityiOS (2.0 and later), macOS (10.0 and later), tvOS (9.0 and later), watchOS (2.0 and later)
Declared InObjective-C
MoreType Method Reference



Steve Christensen <punster@...>
 

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.