title: Objective-C Protocol Conformance Using Subclass in Method Signature
body: <p>I'm probably missing something here as this seems like a pretty basic Objective-C question, but not finding any similar questions (maybe I'm searching the wrong thing, sorry in advance if so!). My question is around enforcing a class that conforms to a protocol to have the correct type in it's method signatures. Since that doesn't seem to be enforced by the compiler, what's the best practice here.</p>
  <p>Let me provide an example.</p>
  <pre><code>// View.h
  
  @protocol View &lt;NSObject&gt;
  
  - (void)createNewViewUsing:(UIView *)view;
  
  @end
  
  // SomeView.h
  
  @interface SomeView : NSObject &lt;View&gt;
  
  @end
  
  - (void)createNewViewUsing:(UIStackView *)view {
      NSLog(@&quot;The view is %@&quot;, view);
      NSLog(@&quot;%@&quot;, view.arrangedSubviews); // CRASH!
  }
  
  // Implementation
  
  SomeView *const view = [[SomeView alloc] init];
  UIView *const anotherView = [[UIView alloc] init];
  [view createNewViewUsing:anotherView];
  </code></pre>
  <p>How do I avoid this crash? Is the correct thing to do to just make sure I'm always matching the method signature as my protocol and then check the type? How do I ensure someone else (or my future self) does this? Why does the compiler not enforce this?</p>