开发者

Does calling a protocol method pass program flow control?

开发者 https://www.devze.com 2023-01-22 12:57 出处:网络
I know this is quite possibly a lame question, but I\'ve pulled three consecutive all-nighters and I\'m very blurry. And I\'m new to Objective C and Cocoa Touch.

I know this is quite possibly a lame question, but I've pulled three consecutive all-nighters and I'm very blurry. And I'm new to Objective C and Cocoa Touch.

I've created a class that provides a delegate method. I'll use simplified example code since the specifics aren't important. The header file looks like this:

#import <Foundation/Foundation.h>

@protocol UsernameCheckerDelegate <NSObject>
@required
- (void)didTheRequestedThing:(BOOL)wasSuccessful;
@end

@interface TheDelegateClass : NSObject {
    id <TheDelegateClassDelegate> tdcDelegate;
}

@property (assign) id <TheDelegateClassDelegate> tdcDelegate;

- (void)methodThatDoesSomething:(int)theValue;

@end

And the source file looks like this:

#import "TheDelegateClass.h"

@implementation TheDelegateClass

@synthesize tdcDelegate;

- (void)methodThatDoesSomethi开发者_StackOverflowng:(int)theValue {
    if (theValue > 10) {
        [[self tdcDelegate] didTheRequestedThing:NO];
        // POINT A
    }

    // POINT B
    int newValue = theValue * 10;
    NSString *subject = [NSString stringWithFormat:@"Hey Bob, %i", newValue];
    // Some more stuff here, send an email or something, whatever

    [[self tdcDelegate] didTheRequestedThing:YES];
    // POINT C
}

@end

Here's my question: if theValue is in fact greater than 10 and the line above POINT A runs, does program flow control pass out of this method (and back to the didTheRequestedThing delegate method in the object that called this) or does flow continue on through POINT B to POINT C?

I'm hoping for the former because I can simplify the heck out of my code, currently an unpleasant mess of deeply nested ifs and elses.


When the -didTheRequestedThing: method returns, control flow returns back to your POINT A and continues on to POINT B and POINT C. Delegate method calls are exactly like any other method call. If you want to avoid executing the rest of the method after the delegate call, just stick a call to return where your // POINT A comment is.

0

精彩评论

暂无评论...
验证码 换一张
取 消