Convert float to NSString and vice versa?

Discussion in 'iPhone/iPad Programming' started by ArtOfWarfare, Aug 20, 2008.

  1. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #1
    timeRemaining is a float,
    timeRemainingLabel is a UILabel,
    roundTime is a UITextField,

    all 3 are declared in my header.

    I'm getting errors with these two lines of code...

    Code:
    timeRemaining = roundTime.text;
    I have no idea how to convert a NSString to a float... I was crossing my fingers this would work... apparently it didn't as I got this error

    Code:
    timeRemainingLabel.text = (@"Time Remaining: %f Seconds", timeRemaining);
    I was actually expecting this one to work... I thought it would work just like in C where %f would be replaced with timeRemaining. Apparently not because I got this error
    Well... at least I havn't had the same problem twice yet... it's a sign I'm learning from my mistakes, right? Good judgement comes from experience; experience comes from bad judgement.

    Thanks for all the help people have been giving, and thanks in advance for help with this too.
     
  2. macrumors 65816

    Joined:
    Jun 29, 2007
    #2
    something = [NSString stringWithFormat:mad:"%f",floatValue];
     
  3. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    Did you even open the documentation? In the time it took you to type that you could have found the floatValue method of NSString.

    As for turning floats into NSStrings you have a few options. The closest to what you have posted (which makes no sense at all, are you expecting the compiler to magically turn that into a method call for you) is also clear in the documentation: the convenience stringWithFormat: factory method.
     
  4. thread starter macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    Sorry, I did check the documentation but I didn't understand how to use stringWithFormat properly.

    Thanks detz for giving an example of how to use stringWithFormat.

    And thanks robbieducan for pointing out the method I overlooked... floatValue.

    It works now... although my timer doesn't seem to be working properly... hopefully I can fix that without asking for help on the forums...

    Edit: Now the timer works. Just for the record... if you're making a timer that repeats every 0.01 seconds... you shouldn't make it send a message to the NSLog every time...
     
  5. macrumors newbie

    Joined:
    Dec 1, 2001
    #5
    That doesn't make you any less of a jackass. Some people don't have the knowledge you do, so please, cut them some slack. Detz's concise response was exactly what I was Googling for.
     
  6. thread starter macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #6
    No, he was completely right. I'd become too dependent on asking people for help rather than taking the time to check the documentation. Don't call him names just for pointing out my flaws... how else would people learn they were doing things wrong? Their ego will tell them they're doing it right so they'll have to have someone else tell them they're wrong.

    This seems rather off topic but if you check the ADC page I'm pretty sure that one is anwsered in the FAQ...

    http://developer.apple.com/iphone/gettingstarted/docs/gettingstartedfaq.action

    I'm not going to give you the exact answer... instead I'll let you learn how to check the adc documentation to find answers yourself.
     
  7. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    Ah, thanks! I was simply ignoring the other dude! I hope your coding is going well :)

    So far off-topic it's got it's own badly titled thread. Although the advice in it is basically the same as yours.
     
  8. macrumors 65816

    Sbrocket

    Joined:
    Jun 3, 2007
    Location:
    /dev/null
    #8
    aaronvegh -

    People answering questions here sometimes come off as rude because the questions asked in those cases are typically rudimentary, with answers that could be readily obtained from a quick search through Apple's doc sets. Using documentation is a key skill that less knowledgeable or developers that are just starting out will find it in their best interest to develop should they want to avoid relying on others to answer every simple question (and probably getting a snappy answer in reply.)
     

Share This Page