gpt4 book ai didi

objective-c - 是否可以手动遍历嵌套的 if 语句?

转载 作者:行者123 更新时间:2023-11-30 14:29:20 26 4
gpt4 key购买 nike

二叉树让我陷入不活跃状态,所以我想我应该尝试一种更简单(如果明显更困惑)的方法。
例如...

if (a) {
do something
// wait for a button press before checking the next 'if'
if (aa) {
do something
} else if (ab) {
do something
}
} else if (b) {
do something else
}

等等。
如何强制我的应用程序在询问“aa”是否返回 true 之前等待按钮按下? (等等。)

开关似乎是一个更干净的选择,所以如果有人有该方法的答案,我很乐意尝试一下。但这是同样的问题。我不知道如何一步步进步而不是一下子进步。

最佳答案

这是一种不同的方法,使用 View 的状态或控制变量来确定按下按钮后下一步应该执行的操作。

// pseudo-code based on your example
-(IBAction) buttonPress1
if (a) {
do something
self.setState = stateA;
} else if (b) {
do something else
self.setState = stateB;
}

-(IBAction) buttonPress2
if (self.state == stateA) {
if (a) {
do something
} else if (b) {
do something
}
}

希望对您有帮助,如果没有,请在评论中提问。

<小时/>

[编辑]

好的,在您解释了 DA/ME/ME2 引用资料后,我明白您在寻找什么了。

您可能想要做的是将您的对话存储在 plist 中,以加载到 NSDictionary 中。 (阅读 NSDictionary)

每个 NSDictionary 应该如下所示:

键=值

@"prompt"= @"嗨,我的名字是鲍勃"@"MEAN"= NSDictionary 对象,用于下一个 convo 选择的平均值@"NICE"= NSDictionary 对象,用于nice的下一个convo选择

const BOOL NICE = YES;
const BOOL MEAN = !NICE;

NSDictionary *convo = //loaded to initial starting point from your plist file

- (IBAction) playerChoseMean:(id)sender
{
[self sayConvo:convo withChoice:MEAN];
}

- (IBAction) playerChoseNice:(id)sender
{
[self sayConvo:convo withChoice:NICE];
}

- (void) sayConvo:(NSDictionary)convo withChoice:(BOOL)b
{
NSLog(@"NPC says: %@", [convo valueForKey:@"prompt"]);
if(b) {
convo = (NSDictionary*)[convo valueForKey:@"NICE"];
} else {
convo = (NSDictionary*)[convo valueForKey:@"MEAN"];
}

if (convo == [NSNull null] || convo == nil) then continue;
//else continue
}

这是一个 NSDictionary 图表示例,可以帮助您入门。

NSDictionary *intro = [NSDictionary dictionaryWithCapacity:3];
NSDictionary *nice = [NSDictionary dictionaryWithCapacity:3];
NSDictionary *nicenice = [NSDictionary dictionaryWithCapacity:3];
NSDictionary *nicegoodbye = [NSDictionary dictionaryWithCapacity:3];
NSDictionary *mean = [NSDictionary dictionaryWithCapacity:3];
NSDictionary *meangoodbye = [NSDictionary dictionaryWithCapacity:3];

[intro addValue:@"hi there!" forKey:prompt];
[intro addValue:nice forKey:@"NICE"];
[intro addValue:mean forKey:@"MEAN"];

[nice addValue:@"that was nice" forKey:prompt];
[nice addValue:nicenice forKey:@"NICE"];
[nice addValue:mean forKey:@"MEAN"];

[nicenice addValue:@"awww" forKey:prompt];
[nicenice addValue:nicegoodbye forKey:@"NICE"];
[nicenice addValue:mean forKey:@"MEAN"];

[nicegoodbye addValue:@"you were super nice, here's a bonus for being so nice" forKey:prompt];
[nicegoodbye addValue:[NSNull null] forKey:@"NICE"];
[nicegoodbye addValue:[NSNull null] forKey:@"MEAN"];

[mean addValue:@"that was mean" forKey:prompt];
[mean addValue:nice forKey:@"NICE"];
[mean addValue:meangoodbye forKey:@"MEAN"];

[meangoodbye addValue:@"you're a jerk! goodbye!" forKey:prompt];
[meangoodbye addValue:[NSNull null] forKey:@"NICE"];
[meangoodbye addValue:[NSNull null] forKey:@"MEAN"];
<小时/>

看看 GameDeve.SE 上这个优秀的例子

关于objective-c - 是否可以手动遍历嵌套的 if 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4795579/

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