Xcode problems

Discussion in 'iOS Programming' started by nagol2013, Jun 11, 2011.

  1. nagol2013, Jun 11, 2011
    Last edited by a moderator: Jun 11, 2011

    nagol2013 macrumors newbie

    Joined:
    Jun 10, 2011
    #1
    Hi, I am working on an application and part of it is to let the user type something in a text field and search it via google. Right now I have it linked to google and when the user presses search its transitions to the webView. My problem is that I can't get xcode to take what the user has typed into the text field and insert it into the google link properly. Right now it just shows a search of "(null)" in the webView. I was wondering if anyone can help me resolve this. Oh by the way this is my first post and I don't know how to put code in properly so I will just copy and paste it.

    Code:
     NSLog(@"%@",searchBarGoogle.text);
     NSString *string1 = [[NSString alloc] initWithFormat:@"%@",searchBarGoogle.text];
     NSString *string = [NSString stringWithFormat:@"http://www.google.com/search?q=%@",string1];
     NSURL *url = [NSURL URLWithString:string];
     NSURLRequest *request = [NSURLRequest requestWithURL:url];
     [webView loadRequest:request];
    Thanks in advance for future help!
     
  2. chown33, Jun 11, 2011
    Last edited: Jun 11, 2011

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Here is the URL for this forum (iPhone/iPad Programming):
    http://forums.macrumors.com/forumdisplay.php?f=135

    Click it.

    At the top of the Threads list are some threads marked Sticky.

    Click the one named "Posting code, please use the
    Code:
     tags".
    
    Follow its instructions.
    
    
    [CODE]NSString *string = [NSString stringWithFormat:@"http://www.google.com/search?q=%@",string1];
    
    After this statement, NSLog the values of both string and string1. Post the output.

    If I had to guess, I would guess that string1 has some characters that are illegal in URLs, and you haven't percent-escaped them. There is an NSString method that will percent-escape things. Look it up in the NSString class reference doc.

    Oh, and it looks like you might be leaking string1.
     
  3. nagol2013 thread starter macrumors newbie

    Joined:
    Jun 10, 2011
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Copy and paste it into a reply post.
     
  5. nagol2013 thread starter macrumors newbie

    Joined:
    Jun 10, 2011
    #5
    is it possible to do an if statement to say that if it returns null then put the user's input?
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    Do you know how to write an if statement?

    What book or tutorial are you using? Be specific.
    Title, author, and edition for a book; website and URL for an online tutorial.


    What have you tried? Be specific.

    1. Post your code, even if it doesn't work.
    2. Describe what you expected to happen.
    3. Describe what actually happened.
    4. Post any error or warning messages, exactly.


    When describing problems, be specific and accurate. I don't know what "it" means where you wrote "if it returns null". I could guess you mean the user's input, but you haven't identified which variables are user inputs or what their types are. Also, "if it returns null" doesn't make logical sense here. What does make sense is "if the user input is NOT null", but that's the opposite of what you wrote. Accuracy is important.
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Also, be aware that your thread title is a little misleading, as is this statement:
    The problem is not with Xcode, which is just a tool to work with your code; the problem lies with Objective-C, and your understanding of how to use it.

    Hope that helps.
     
  8. jnoxx macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #8
    I think you need to redefine your questions more properly, for us to debug it.
    Code:
     NSLog(@"%@",searchBarGoogle.text);
     NSString *string1 = [[NSString alloc] initWithFormat:@"%@",searchBarGoogle.text];
     NSString *string = [NSString stringWithFormat:@"http://www.google.com/search?q=%@",string1];
     NSURL *url = [NSURL URLWithString:string];
     NSURLRequest *request = [NSURLRequest requestWithURL:url];
     [webView loadRequest:request];
    
    first u log your searchBarGoogle.text -> I guess that's an UITextField.
    But if it's nil here, then that's the first error. because after you are making a new string (imo, just use [NSString stringWithFormat:] like the one below your second line. This is autoreleased, and will cause u less headeaches.
    The URL creating is correct, and loading it into a URL too.
    The problem is purely based on your textfield, and getting the right info from there.

    So, how and when do you do that ;)
     

Share This Page