-2
#import "ApiService.h"

@implementation ApiService
static ApiService *sharedInstance = nil;

+ (ApiService *)sharedInstance
{
    if (sharedInstance == nil)
    {
        sharedInstance =  [[self alloc]init];
    }

    return sharedInstance;
}

- (id)init
{
    if (self = [super init])
    {
    }
    return self;
}
@end

When I call +sharedInstance what does self refer to? How am I allowed to call init from a Class method?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Chris Muench
  • 17,444
  • 70
  • 209
  • 362
  • Check out this question for a better way to make a singleton: http://stackoverflow.com/questions/5720029/create-singleton-using-gcds-dispatch-once-in-objective-c – Eric Jan 28 '13 at 16:36
  • possible duplicate of [Refering to the class itself from within a class mehod in Objective C](http://stackoverflow.com/questions/2255237/refering-to-the-class-itself-from-within-a-class-mehod-in-objective-c) – vikingosegundo Jan 28 '13 at 16:50
  • https://developer.apple.com/library/ios/#documentation/General/Conceptual/DevPedia-CocoaCore/ClassMethod.html – vikingosegundo Jan 28 '13 at 16:52

1 Answers1

2

self is the class.

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

Is the same as:

+ (id)create {
  return [[SomeClass alloc] init];
}

Or in your example:

+ (ApiService *)sharedInstance
{
    if (sharedInstance == nil)
    {
        sharedInstance =  [[ApiService alloc]init];
    }

    return sharedInstance;
}

This allows you to call class methods on self from class methods. And it allows you to call them on the child class when you have inheritance, as class methods are inherited too.

Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • If self is the class, then when you do things like: self.property, how can that be the class? unless the property is static? – Rob Jan 28 '13 at 16:52
  • @Rob, indeed there are 2 selfs in objective-c, check https://developer.apple.com/library/ios/#documentation/General/Conceptual/DevPedia-CocoaCore/ClassMethod.html – vikingosegundo Jan 28 '13 at 16:56
  • or better phrased: classes are objects themselves and as such they can send messages to themselves via `self`. http://stackoverflow.com/questions/5773054/are-classes-objects-in-objective-c – vikingosegundo Jan 28 '13 at 17:11
  • looked at the link, that is strange, in C++ or Java, you certainly would not use this terminology, e.g. this is not the class... – Rob Jan 28 '13 at 17:13
  • because classes don't have properties, unless they are properties that apply to all instances.. ? – Rob Jan 28 '13 at 17:15
  • @Rob: properties is not the only use for self. – vikingosegundo Jan 28 '13 at 17:15
  • and there is nothing like static or class properties. – vikingosegundo Jan 28 '13 at 17:16
  • Self from a class method is the class, allowing you to call class methods. Self from an instance method is the instance allowing you to call instance methods. – Alex Wayne Jan 28 '13 at 17:17
  • 1
    It has practical reasons to use self in class methods. In a inheritance scenario, using "self" instead of "ApiService" will guarantee that subclass of ApiService don't have to override "sharedInstance" method and be able to get the subclass object correctly. This trick is actually mentioned in Apple's programming guide https://developer.apple.com/library/mac/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/WorkingwithObjects/WorkingwithObjects.html. At very bottom, Tip: Rather than using [[XYZPerson alloc] init] in the class factory method, instead try using [[self alloc] init]. – dalef Nov 13 '13 at 20:59