使用If语句,获取一个String,以传递给API调用

时间:2014-06-14 17:12:29

标签: ios api if-statement

我需要将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];

1 个答案:

答案 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"];
}

这将解决您的问题。