Note that there are some explanatory texts on larger screens.

plurals
  1. POiPhone SDK - Converting my delegate based function to blocks
    text
    copied!<p>I have a very simple class (currently used as a testing class), which uses <code>delegate/protocol</code> methods to interface with it's parent class. However, I would really like to convert this to use <code>blocks</code>. Yet I can't find a good resource or tutorial out there to help me figure out how to do this. All the blocks tutorials are just way to complicated, and I would really just like a small, concise example of how to do this.</p> <p>I currently have the class:</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @protocol TestObjectDelegate &lt;NSObject&gt; @optional -(void)testObjectSucceeded:(BOOL)passedTest; -(void)testObjectedFailed:(NSError *)error; @end @interface TestObject : NSObject { id&lt;TestObjectDelegate&gt; _delegate; } -(void)compare:(NSString *)stringA with:(NSString *)stringB; @end #import "TestObject.h" @implementation TestObject - (id)initWithDelegateController:(id&lt;TestObjectDelegate&gt;)delegate { self = [super init]; if (self) { _delegate = delegate; } return self; } -(void)compare:(NSString *)stringA with:(NSString *)stringB { if ([stringA isEqualToString:stringB]) { if(_delegate &amp;&amp; [_delegate respondsToSelector:@selector(testObjectSucceeded:)]) { [_delegate testObjectSucceeded:YES]; } else { [_delegate testObjectSucceeded:NO]; } } else { if(_delegate &amp;&amp; [_delegate respondsToSelector:@selector(testObjectedFailed:)]) { [_delegate testObjectedFailed:nil]; } } } @end </code></pre> <p>How could I begin to convert this to a blocks based function? Also, I know 'retain cycles' are something to watch out for when implementing a blocks function. What would I need to watch out for when converting this class to use blocks instead of delegate/protocols? Googling 'retain cycles' also gives some overly complicated answers.</p> <p>Any starting pointers would be much appreciated?</p>
 

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