title: Objective-C class method local variables of type "self"
body: <p>We're all familiar with the following pattern for instantiating instances of a class:</p>
  
  <pre><code>+ (instancetype)createInstance {
      return [[self alloc] init];
  }
  </code></pre>
  
  <p>This works because "self" in this case refers to the class rather than an object built from the class blueprint.</p>
  
  <p>We also know this declaration, most commonly used in avoiding retain cycles:</p>
  
  <pre><code>typeof(self) someStrongSelf = self;
  </code></pre>
  
  <p>This is allows <code>self</code>'s type to be dynamic and that code could be copy-pasted wherever needed no matter the class.</p>
  
  <p>My question deals with combining the above two patterns when instantiating from a class method:</p>
  
  <pre><code>+ (instancetype)createInstance:(MyObject*)dependency {
      typeof(self) instance = [[self alloc] init];
      instance.dependency = dependency;
  
      return instance;
  }
  </code></pre>
  
  <p>This won't work because <code>self</code> is a class, and a <code>typeof(class)</code> is just a <code>Class</code>, but is there some mechanism for local variables equivalent to <code>instancetype</code> that would allow me the same flexibility as typeof(instance)? For example:</p>
  
  <pre><code>+ (instancetype)createInstance:(MyObject*)dependency {
      instanceof(self) instance = [[self alloc] init]; //desired keyword
      instance.dependency = dependency;
  
      return instance;
  }
  </code></pre>
  
  <p>If I really wanted this formalized, I know an alternative to this would be defining a protocol that does basically the same thing as above, but I'm curious if Objective-C allows the desired declaration style out of the box.</p>