Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Defining such a category on all such classes in general is not easily solvable. But your actual problem seems simpler: How does one add a category method to <code>NSManagedObject</code> for use only with subclasses that implement <code>&lt;SOManagedObject&gt;</code>? This is solvable.</p> <p>What you want to do is add the method to <code>NSManagedObject</code>, then check that the instance you're working with can handle the messages you want to send it from <code>&lt;SOManagedObject&gt;</code>.</p> <p>Let us suppose that we are given:</p> <pre><code>/* SOManagedObject.h */ @protocol SOManagedObject - (void)frobble_so; - (void)bobble_so; @end </code></pre> <p>Now let's add a category method to all <code>NSManagedObject</code> subclasses that implement <code>SOManagedObject</code>:</p> <pre><code>/* NSManagedObject+SOConvenience.h */ @interface NSManagedObject (SOConvience) /* Frobbles and bobbles - no effect on non-&lt;SOManagedObject&gt;-conforming * instances. */ - (void)frobbleAndBobble_so; @end </code></pre> <p>To do so, we implement the method like so:</p> <pre><code>/* NSManagedObject+SOConvenience.m */ @implementation NSManagedObject (SOConvenience) - (void)frobbleAndBobble_so { if (![self conformsToProtocol:@protocol(SOManagedObject)]) return; NSLog(@"%s: Thunderbirds are GO! Let's frobble and bobble!", __func__); [self frobble_so]; [self bobble_so]; } @end </code></pre> <p>You could optionally assert to ensure you are not sending the method to the wrong objects, or you could use <code>respondsToSelector:</code> instead of checking for protocol conformance. Whatever floats your boat, but this general tack should take you where you want to go.</p>
    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.
    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