Date
1 - 12 of 12
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:
toggle quoted message
Show quoted text
static id instance; instead? - Jon On Aug 30, 2017, at 2:37 PM, Alex Zavatone <zav@...> wrote: |
|||||||||||
|
|||||||||||
Alex Zavatone
I wish.
toggle quoted message
Show quoted text
Pointer to non-const type ‘id’ with no explicit ownership.
|
|||||||||||
|
|||||||||||
Jon Gotow
On Aug 30, 2017, at 2:42 PM, Alex Zavatone <zav@...> wrote: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
toggle quoted message
Show quoted text
On Aug 30, 2017, at 3:49 PM, Jon Gotow <gotow@...> wrote:On Aug 30, 2017, at 2:42 PM, Alex Zavatone <zav@...> wrote:No no - not: |
|||||||||||
|
|||||||||||
On 30 Aug 2017, at 1:37 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)? 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:It makes the code copy/paste-able. No dependency on the name of class you are adding it to.On 30 Aug 2017, at 1:37 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)? |
|||||||||||
|
|||||||||||
On 30 Aug 2017, at 1:55 pm, Alex Zavatone <zav@...> wrote: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?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. (By "un-typed" mean in the language sense, not the keyboard sense; pun unintended.) -ben |
|||||||||||
|
|||||||||||
Alex Zavatone
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? :)
toggle quoted message
Show quoted text
|
|||||||||||
|
|||||||||||
Alex Zavatone
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.
|
|||||||||||
|
|||||||||||
Steve Christensen <punster@...>
On Aug 30, 2017, at 2:44 PM, Alex Zavatone <zav@...> wrote:
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. |
|||||||||||
|