IDE error: Variable undefined but defined in an if statement - ios

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.

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.

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.

Related

Can't set String to URLWithString

I'm having an issue with a action to a Facebook page.
The currentVriend is a NSObject Class.
I get the error: too many arguments to method call expected 1 have 2
-(void)goFB
{
NSURL *url = [NSURL URLWithString:#"fb://profile/%g",self.currentVriend.fbid];
[[UIApplication sharedApplication] openURL:url];
}
What am I'm doing wrong?
Maybe make another String?
Thanks
I believe you are missing the correct NSString method
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"fb://profile/%g",self.currentVriend.fbid]];

IOS - How do I add segments to NSURL?

Suppose you have a URL that looks like http://localhost:5867 and your wanted to append '/something' to the url. How do I append segments to this URL?
I am trying this:
//_baseURL is NSURL* and documentid is NSString*
NSURL* url = [_baseURL URLByAppendingPathComponent:documentid];
Oddly.. xcode(4.6.2) will not output the resulting URL to console.
NSLog(#"%#", [url absoluteString]);//nothing output
Thanks!
Edit
I am working in a workspace. The code that I am debugging is a static lib which is in the workspace. Could this be the culprit of all this weirdness??
Fixed
I think the reason I was having problems was because my workspace was not configured correctly and I was actually executing an older version of my static lib. This explains why I wasn't getting NSLog'ing and why the result were unexpected. I found this post which helped me understand the workspace and how to configure it for a static library. Thanks a lot for everyone's time!
There is a class message,
+ (id)URLWithString:(NSString *)URLString relativeToURL:(NSURL *)baseURL
So,
NSURL *base = [NSURL URLWithString: #"http://localhost:5867"];
NSURL *child = [NSURL URLWithString: #"something" relativeToURL:base];
NSURL *base = [NSURL URLWithString:#"http://localhost:5867"];
NSURL *url = [base URLByAppendingPathComponent:#"something"];
NSLog(#"%#", [url absoluteString]);
This works for me and prints http://localhost:5867/something, so check your variables (maybe one of them is nil or an empty string?).

How to make NSURL variable is nil when parsed from null String?

I'm a newbie in iOS development. Sometimes my API server returns a nil value. This is raising an error when I try to create a NSURL instance.
Here is the condition:
NSString *tmpURL = nil;
NSURL *url = [NSURL URLFromString:tmpURL];
In this condition, it will make the app crash. I just want to make the url variable nil, not raise an error.
How about:
NSString *tmpURL = nil;
NSURL *url = tmpUrl ? [NSURL URLFromString:tmpURL] : nil;

How does one append something like "?x=123" to an NSURL?

I have an NSURL of the form
"http://abc.def.com:1234/stuff/"
and I want to append to it so the final url is like this
"http://abc.def.com:1234/stuff/?x=123".
But if I do this
url = [NSURL URLWithString:#"http://abc.def.com:1234/stuff/?"];
url = [url URLByAppendingPathComponent:#"x=123"];
Then the result is
http://abc.def.com:1234/stuff/x=123?
(its the same result if URLByAppendingPathExtension is used).
But if I do this
url = [NSURL URLWithString:#"http://abc.def.com:1234/stuff/?"];
url = [url URLByAppendingPathComponent:#"x=123"];
Then the result is
http://abc.def.com:1234/stuff/%3Fx=123
(also the same result if URLByAppendingPathExtension is used).
Neither of which is what I am after. How do I get a final result of "http://abc.def.com:1234/stuff/?x=123" ?
I think the simplest answer here is to create a new URL using the old one as the base URL.
url = [NSURL URLWithString:#"?x=123" relativeToURL:url]
Here is a unit test that checks the logic
- (void)testCreatingQueryURLfromBaseURL
{
NSURL *url = [NSURL URLWithString:#"http://foo.com/bar/baz"];
url = [NSURL URLWithString:#"?x=1&y=2&z=3" relativeToURL:url];
STAssertEqualObjects([url absoluteString], #"http://foo.com/bar/baz?x=1&y=2&z=3", nil);
}
Create your actual NSURL object last, from an NSString that you build up as needed:
NSString *urlString = #"http://www.site.com/";
if (some condition)
urlString = [urlString stringByAppendingString:#"?x=123"];
NSURL *url = [NSURL URLWithString:urlString];

NSURL URLWithString the url is always nil

My code is like this :
NSString *urlStr = [NSString stringWithFormat:#"https://xxx.xxx.xx.xx/Pages/Service/FMService.svc/FileAudit?user=%#&pass=%#&fileId=%d&isAudited=1&opinion=%#",appDelegate.user.userName,appDelegate.user.password,self.todo.fileId,self.opinion];
NSLog(#"URL=%#",urlStr);
NSURL *url = [NSURL URLWithString:urlStr];
And the url = (NSURL *) 0*00000000#"< nil >"
how can I work it out? thank you in advance!
For debugging purposes, I would build up urlStr piece by piece instead of combining five NSStrings all at once. Put in an NSLog(#"%#", urlStr) statement after each addition to urlStr. This will reveal which piece of the puzzle is causing the problem.

Resources