如何从 "for loop" 到 return 字符串? Objective-C iOS

How to return string from "for loop"? Objective-C iOS

我是 Objective-C 语言编程的新手。

我需要 return this 来自 for loop 的“tabName”函数。如何比较app_additional_tab_name_等于tabName?因为现在仍然是 return 任何 app_additional_tab_name_ 的姓氏。

+ (NSString *)additionalTabName {
    for (int i = 0; i <= 16; i++) {
    tabNameConcatString = [NSMutableString stringWithFormat:@"app_additional_tab_name_%d", i];
    tabName = [LocalizedStringsUserDefaults valueForKey:tabNameConcatString];
        if ([LocalizedStringsUserDefaults existsObjectForKey:tabNameConcatString])  {
            tabName = [LocalizedStringsUserDefaults valueForKey:tabNameConcatString];
            NSLog(@"This tabName is: @% @% @% @%", tabName, @" and will append to: ", tabNameConcatString);
        }
    }
    return tabName;
}

祝一切顺利。

将return移入匹配条件

+ (NSString *)additionalTabName {
    for (int i = 0; i <= 16; i++) {
    tabNameConcatString = [NSMutableString stringWithFormat:@"app_additional_tab_name_%d", i];
    tabName = [LocalizedStringsUserDefaults valueForKey:tabNameConcatString];
        if ([LocalizedStringsUserDefaults existsObjectForKey:tabNameConcatString])  {
            tabName = [LocalizedStringsUserDefaults valueForKey:tabNameConcatString];
            NSLog(@"This tabName is: @% @% @% @%", tabName, @" and will append to: ", tabNameConcatString);
            return tabName;
        }
    }
    return nil;
}

如果没有找到匹配项,您可以 return nil 或 return 空字符串。