Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Strictly speaking, there is no "abstract class" or "abstract property" in Objective-C, see for example this thread <a href="https://stackoverflow.com/questions/1034373/creating-an-abstract-class-in-objective-c">Creating an abstract class in Objective-C</a> for a good overview.</p> <p>Your approach is not optimal, because it requires that the superclass implements <code>foo</code> and <code>setFoo:</code>, which contradicts the idea of "abstractness".</p> <p>A better solution is to define a "dynamic property" in the superclass:</p> <pre><code>@interface SuperClass : NSObject @property (strong, nonatomic) Foo *foo; @end @implementation SuperClass @dynamic foo; @end </code></pre> <p>and explicitly synthesize it in the subclass:</p> <pre><code>@interface SubClass : SuperClass @end @implementation SubClass @synthesize foo = _foo; @end </code></pre> <p>Now you can access <code>foo</code> on a subclass object, but on a superclass object it will cause a runtime exception.</p> <p>For lazy initialization, you can use the usual pattern, without any "super-tricks":</p> <pre><code>- (Foo *) foo { if(!_foo) _foo = [[Foo alloc] init]; return _foo; } </code></pre> <p>An <strong>alternative approach</strong> (also mentioned in above thread) is to use a "protocol" instead of a common superclass:</p> <pre><code>@protocol HasFoo &lt;NSObject&gt; - (Foo *)foo; @end @interface MyClass : NSObject&lt;HasFoo&gt; @property(strong, nonatomic) Foo *foo; @end @implementation SubClass - (Foo *) foo { if(!_foo) _foo = [[Foo alloc] init]; return _foo; } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload