Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

drf1229

macrumors regular
Original poster
Jun 22, 2009
237
0
I guess I really need to brush up on my basics. I seem to be having trouble with concatenating strings. My issue:

This doesn't work:
Code:
NSString *txt=@"MyPost";
	NSString *url=[NSString stringWithFormat:@"mydomain.com/newfeedbackiphone.php?text=%@",txt];
	NSString *aString = [[NSString alloc] initWithContentsOfURL:[[NSURL alloc] initWithString:url]];

Yet strangely this does:
Code:
	NSString *aString = [[NSString alloc] initWithContentsOfURL:[[NSURL alloc] initWithString:[NSString stringWithFormat:@"mydomain.com/newfeedbackiphone.php?text=MyPost",txt];]];

What i mean by works and doesn't is in the first one, aString initWithContentsOfURL returns nothing, yet it returns what it is supposed to in the second. Am I doing something wrong with the strings, or could it be my URL connection I need to diagnose? Thanks for any advice!
 
Do you mean url or aString?
In the correct one, aString returns what it should in debugger. In the incorrect one it returns absolutely nothing. (not even a blank space)
url returns mydomain.com/newfeedbackiphone.php?text=MyPost in debugger both times.
 
Wow, now I feel like an idiot. I found the problem... I mistyped my domain in the "incorrect" one. Can't believe I missed that. I thought I double checked but I guess I should've triple checked. Thanks for your help anyway!
 
I found another issue I was experiencing as well. My post had a space in it, so that completely invalidated the url. I forgot about that for a second.
 
I guess I really need to brush up on my basics.
That might be a good idea. Because, besides the other issues (that it seems you've solved), you are doing plenty of allocs with no way to release. That's poor memory management. As well, NSString has two instance methods involving initWithContentsOfURL, initWithContentsOfURL:encoding:error: and initWithContentsOfURL:usedEncoding:error:, neither of which you are using here.
 
As well, NSString has two instance methods involving initWithContentsOfURL, initWithContentsOfURL:encoding:error: and initWithContentsOfURL:usedEncoding:error:, neither of which you are using here.
Funny thing you should mention that. I just added this line right before I connect with the URL:
Code:
url=[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Is that doing the same thing?
That might be a good idea. Because, besides the other issues (that it seems you've solved), you are doing plenty of allocs with no way to release.
What do you mean no way to release? I guess I haven't really payed as much attention to memory allocation as I should.
 
You are allocating NSString, NSURLRequest and NSURL without a pointer meaning that you can not release them manually.
 
Funny thing you should mention that. I just added this line right before I connect with the URL:
Code:
url=[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Is that doing the same thing?
No, that just encodes the URL that will be sent (the request). You'll also want to set the encoding for the data you get back (the response). Also, don't forget that using initWithContentsOfURL is synchronous, meaning your UI, etc. will be locked up while it's working. PhoneyDeveloper suggested you look into using NSURLConnection in another thread. I'm gonna echo that advice.
 
Some of the other problems with initWithContentsOfURL are that error reporting is just a nil value returned and you can't cancel the request. If you use the async NSURLConnection method you get better error handling and the ability to cancel.
 
Ok, I understand NSURLConnection is the way to go. Now I also get what encoding the entire request does. Thanks for all your advice (dejo, Kool, and phoney)!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.