iosobjective-cif-statement

Using If statement, to get one String, to pass into API call


I need to pass a string variable into an API call, but I'm having a hard time getting the string from an if statement.

Below is what I've tried so far, but I get a warning that "usersID is an unused variable" inside each of the if statements...

Which makes sense, and is the main problem I'm trying to solve for (i.e. run thru an if statement to get one string to use after evaluation).

I will post any extra code as needed!

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];

Solution

  • You must have to declare your variable before if statments, use below insetead:

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

    This will fix your concern.