我需要将string
变量传递给API
来电,但我很难从string
获取if
言。
以下是我迄今为止尝试过的内容,但我收到警告," usersID
是未使用的变量"在每个if
语句中......
这是有道理的,也是我试图解决的主要问题(即通过if
语句运行以在评估后使用string
。
感谢您的帮助,将根据需要发布任何额外的代码!
ViewController.h
@property (nonatomic, strong) NSString *usersID;
// Get leaf and theme from Saved defaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *leafAbbreviation = [defaults objectForKey:[NSString stringWithFormat:@"leafAbbreviation"]];
NSString *themeID = [defaults objectForKey:[NSString stringWithFormat:@"themeId"]];
// Get ID User number based on Leaf and Theme
if ([leafAbbreviation isEqualToString: @"new"] && [themeID isEqualToString: @"2"]) {
NSString *usersID = [NSString stringWithFormat:@"728"];
}
else if ([leafAbbreviation isEqualToString: @"new"] && [themeID isEqualToString: @"3"]) {
NSString *usersID = [NSString stringWithFormat:@"275"];
}
else {
NSString *usersID = [NSString stringWithFormat:@"486"];
// API
NSString *path = [NSString stringWithFormat:@"v1/%@/media/?client_id=xxx", usersID];
答案 0 :(得分:2)
你必须在if语句之前声明你的变量,而不是在下面使用:
// Get ID User number based on Leaf and Theme
NSString *usersID = [NSString stringWithFormat:@""];
if ([leafAbbreviation isEqualToString: @"new"] && [themeID isEqualToString: @"2"]) {
usersID = [NSString stringWithFormat:@"728"];
}
else if ([leafAbbreviation isEqualToString: @"new"] && [themeID isEqualToString: @"3"]) {
usersID = [NSString stringWithFormat:@"275"];
}
else {
usersID = [NSString stringWithFormat:@"486"];
}
这将解决您的问题。