gpt4 book ai didi

iphone - 调用协议(protocol)方法是否通过程序流控制?

转载 作者:行者123 更新时间:2023-12-01 18:03:19 24 4
gpt4 key购买 nike

我知道这很可能是一个蹩脚的问题,但我已经连续三个通宵了,我很模糊。我是 Objective C 和 Cocoa Touch 的新手。

我创建了一个提供委托(delegate)方法的类。我将使用简化的示例代码,因为细节并不重要。头文件如下所示:

#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

源文件如下所示:
#import "TheDelegateClass.h"

@implementation TheDelegateClass

@synthesize tdcDelegate;

- (void)methodThatDoesSomething:(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

这是我的问题:如果 值(value) 实际上大于 10 并且 POINT A 上方的行运行,程序流控制是否传递出此方法(并返回到调用此方法的对象中的 didTheRequestedThing 委托(delegate)方法)或流程是否继续B点到C点?

我希望是前者,因为我可以简化我的代码,目前是深度嵌套的 if 和 else 令人不快的困惑。

最佳答案

当 -didTheRequestedThing: 方法返回时,控制流返回到您的 POINT A 并继续到 POINT B 和 POINT C。委托(delegate)方法调用与任何其他方法调用完全相同。如果您想避免在委托(delegate)调用后执行该方法的其余部分,只需调用 return您的//POINT A 评论在哪里。

关于iphone - 调用协议(protocol)方法是否通过程序流控制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4039862/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com