Basic string problem

Discussion in 'iOS Programming' started by drf1229, Jan 5, 2010.

  1. drf1229 macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #1
    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!
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    Why don't you inspect the two strings in the debugger or print them. How are they different?
     
  3. drf1229 thread starter macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #3
    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.
     
  4. drf1229 thread starter macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #4
    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!
     
  5. drf1229 thread starter macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #5
    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.
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    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.
     
  7. drf1229 thread starter macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #7
    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?
    What do you mean no way to release? I guess I haven't really payed as much attention to memory allocation as I should.
     
  8. KoolStar macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #8
    You are allocating NSString, NSURLRequest and NSURL without a pointer meaning that you can not release them manually.
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    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.
     
  10. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #10
    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.
     
  11. drf1229 thread starter macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #11
    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)!
     

Share This Page