Need help with string concatenation please

Discussion in 'iOS Programming' started by guyddor, Aug 3, 2009.

  1. guyddor macrumors member

    Joined:
    Jan 7, 2009
    #1
    Hi,

    I want to develop an app that will take the value from UITextField and use it
    in a url like that:

    http://www. the value from UITextField .com/

    How do I do that?
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Use NSString's stringByAppendingString: or stringByAppendingFormat:
     
  3. zrbecker macrumors member

    Joined:
    Jun 28, 2009
    #3
    This should work.

    Code:
    NSString urlString = [NSString stringWithFormat:"http://www.%@.com/", myUITextBox.text];
     
  4. moral-hazard macrumors regular

    Joined:
    Jul 27, 2009
    Location:
    Palo Alto, CA
    #4
    I'll second this approach (with the minor correction I added :p). +stringWithFormat is good because it gives you an autoreleased string.
     
  5. guyddor thread starter macrumors member

    Joined:
    Jan 7, 2009
    #5
    Thanks! but when I put the code...

    The button get stuck

    What to do?
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Explain the issue in more detail. Like, what do you mean the button got "stuck"? Any errors or warning, run-time or build? Perhaps include the button-action code...
     
  7. guyddor thread starter macrumors member

    Joined:
    Jan 7, 2009
    #7
    I don't get warnings but...

    Look at the attachment file
     

    Attached Files:

  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Restated.
     
  9. moral-hazard macrumors regular

    Joined:
    Jul 27, 2009
    Location:
    Palo Alto, CA
    #9
    Your program appears to be crashing. Check the stack trace. I've only seen buttons get stuck like that on a crash.
     
  10. guyddor thread starter macrumors member

    Joined:
    Jan 7, 2009
    #10
    I don't get any warnings

    What to do?

    Edit:

    I got 2 warnings:

    -passing argument 1 of 'stringWithFormat': from incompatible pointer type

    -unused variable 'urlString'
     
  11. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    Keep asking for help without providing any code? ;)
     
  12. guyddor thread starter macrumors member

    Joined:
    Jan 7, 2009
    #12
    Here the MainView.m file code

    What to do?

    This is the only code that I have in my project
     
  13. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #13
    Two things.
    1) You need to prefix your string with the @ character for Objective-C to treat it as a constant NSString object.
    Code:
    NSString *urlString = [NSString stringWithFormat:[COLOR="Red"]@[/COLOR]"http://www.%@.com/", userTextField.text];
    2) Use [ code ] tags rather than quotes.
     
  14. guyddor thread starter macrumors member

    Joined:
    Jan 7, 2009
    #14
    Now I get the warning:

    warning: unused variable 'urlString'

    What should I do?
     
  15. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #15
    Step away from the real coding, go (re)learn the basics of Objective-C and iPhone dev before you continue. Also, it helps to post specific questions rather than generic ones like "What should I do?" Hope that helps.
     

Share This Page