NSString stringWithFormat woes

Discussion in 'iOS Programming' started by RAMilewski, Sep 2, 2008.

  1. macrumors newbie

    Joined:
    Sep 25, 2006
    Location:
    Northern California
    #1
    I'm one of the thousands of procedural programmers lured by the promise of the iPhone platform into the maze of twisty little passages, all the same, that is object oriented Objective-C on Xcode. Like many others, I'm attempting this without a net, and in the absence of a resident necromancer, staff daemonologist, or even a consulting exorcist.

    Frankly, it's bewildering. Here's the conundrum du jour:

    Code:
    NSString *myString = @"http://www.domain.tld";
    myLabel.text=myString;
    This displays http://www.domain.tld.

    ...but alas, I need to build a querystring for my url. Consulting the entrails of a sacrificed chicken (and the Xcode docs and a couple of books on Objective C and Xcode), it appeared that the answer was to use stringWithFormat. However, the following code sample indicates that was naive in the extreme:

    Code:
    NSString *myString = @"http://www.domain.tld";
    float myValue = 4;
    NSString *result = [NSString stringWithFormat:@"%S?query=%.3f",myString,myValue];
    myLabel.text=result;
    This (of course!) displays two Chinese characters and a square box followed by ?query=4.000.

    Being a bear of very little brain, I also tried @"%s?query%.3f" as the format string with similarly dismal results.

    Can anyone point me at some documentation on how to concatenate strings in a useful way in the iPhone environment?
     
  2. macrumors 6502a

    Joined:
    Nov 9, 2007
    #2
    You're using the wrong format specifier for the first part of your URL - your string is an object of type NSString and as such you should be using the id format specifier which is %@.

    Code:
    NSString *myString = @"http://www.domain.tld";
    myLabel.text= [NSString stringWithFormat:@"%@?query=%.3f", myString, 4.0];
    
    Not sure the extra temps buy you much.
     
  3. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    Have you read the documentation?

    %S is used for a "Null-terminated array of 16-bit Unicode characters". And %s is used for a "Null-terminated array of 8-bit unsigned characters. %s interprets its input in the system encoding rather than, for example, UTF-8.". The key being that these are arrays, not objects.

    Right at the top? %@: "Objective-C object, printed as the string returned by descriptionWithLocale: if available, or description otherwise". That sounds a bit more like a NSString. It's an Objective-C object after all.

    Code:
    NSString *myString = @"http://www.domain.tld";
    float myValue = 4;
    NSString *result = [NSString stringWithFormat:@"%@?query=%.3f",myString,myValue];
    myLabel.text=result;
    
    Oh, and can you wrap your code in code tags? It's much easier to read that way.
     
  4. macrumors newbie

    Joined:
    Dec 16, 2009
    #4
    If you wanted to use %s, you could get the UTF8String representation of myString:

    Code:
    NSString *result = [NSString stringWithFormat:@"%s?query=%.3f",[myString UTF8String],myValue];
    
    That's a lower-case "s" there by the way.
     
  5. macrumors 6502a

    Joined:
    Nov 9, 2007
    #5
    Why would you do this?
     

Share This Page