Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's an example of how you would accomplish such a task:</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; typedef int (^IntBlock)(); @interface myobj : NSObject { IntBlock compare; } @property(readwrite, copy) IntBlock compare; @end @implementation myobj @synthesize compare; - (void)dealloc { // need to release the block since the property was declared copy. (for heap // allocated blocks this prevents a potential leak, for compiler-optimized // stack blocks it is a no-op) // Note that for ARC, this is unnecessary, as with all properties, the memory management is handled for you. [compare release]; [super dealloc]; } @end int main () { @autoreleasepool { myobj *ob = [[myobj alloc] init]; ob.compare = ^ { return rand(); }; NSLog(@"%i", ob.compare()); // if not ARC [ob release]; } return 0; } </code></pre> <p>Now, the only thing that would need to change if you needed to change the type of compare would be the <code>typedef int (^IntBlock)()</code>. If you need to pass two objects to it, change it to this: <code>typedef int (^IntBlock)(id, id)</code>, and change your block to: </p> <pre><code>^ (id obj1, id obj2) { return rand(); }; </code></pre> <p>I hope this helps.</p> <p>EDIT March 12, 2012:</p> <p>For ARC, there are no specific changes required, as ARC will manage the blocks for you as long as they are defined as copy. You do not need to set the property to nil in your destructor, either. </p> <p>For more reading, please check out this document: <a href="http://clang.llvm.org/docs/AutomaticReferenceCounting.html" rel="noreferrer">http://clang.llvm.org/docs/AutomaticReferenceCounting.html</a></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. 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