0

I am defining the variable NSURL *url in a series of else if's where there cannot be an error so it will always be defined (I can add error catching later just in case).

This is a horrible way of doing this but im fairly new to the language.

if (whatTime == @"1000") {
        NSURL *url = [NSURL URLWithString:@"http://yyy/json.php?f=1&time=1000"];
    }else if(whatTime == @"1100"){
        NSURL *url = [NSURL URLWithString:@"http://yyy/json.php?f=1"];
    }else if(whatTime == @"1100"){
        NSURL *url = [NSURL URLWithString:@"http://yyy/json.php?f=1"];
    }

However the debugger is telling me url is undefined. Is there a way to get around this?

When I do (above the hell-pit of ifs) NSURL *url; it runs but says it is not connected to the internet.

8
  • Are you sure you are stepping over the appropriate line in the debugger before inspecting the variable? Commented Apr 12, 2013 at 14:18
  • 1
    Side note - you must replace all of the == operators with calls to the isEqualToString: method calls. Example: if ([whatTime isEqualToString:@"1000"]). Commented Apr 12, 2013 at 14:26
  • 1
    Side note: can this whole mess be replaced with one line: NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://yyy/json.php?f=1&time=", whatTime]];? Commented Apr 12, 2013 at 14:29
  • @maddy thanks for that - I will try it. As I say I am new to obj c, coming from a c background I was unsure how concatenation works - thanks! Commented Apr 12, 2013 at 14:40
  • 1
    The %@ format specifier is for Objective-C objects. Otherwise you use the same format specifiers as printf (such as %d and %f, etc.). See the docs for stringWithFormat: for full details. Commented Apr 12, 2013 at 14:55

2 Answers 2

1

For the undefined problem, you have to declare NSUrl *url outside the if/elses:

NSURL *url = nil;
if (whatTime == @"1000") {
    url = [NSURL URLWithString:@"http://yyy/json.php?f=1&time=1000"];
}else if(whatTime == @"1100"){
    url = [NSURL URLWithString:@"http://yyy/json.php?f=1"];
}else if(whatTime == @"1100"){
    url = [NSURL URLWithString:@"http://yyy/json.php?f=1"];
}

I think the not connected problem is not related to that code.

Sign up to request clarification or add additional context in comments.

1 Comment

Make sure you initialize url to nil or add a final else (with no if).
1

The local variables declared inside the if and else if clauses are only visible inside those scopes and therefore you can't use them after the last else if. By declaring NSURL *url; before the if it will be visible after the last else if since it is in the same scope.

The "not connected to the internet" error is probably not related to this code. Probably your device/simulator has no connection.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.