PDA

View Full Version : NSString stringWithFormat woes




RAMilewski
Sep 2, 2008, 04:00 AM
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:

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:

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?



Luke Redpath
Sep 2, 2008, 04:04 AM
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 %@.


NSString *myString = @"http://www.domain.tld";
myLabel.text= [NSString stringWithFormat:@"%@?query=%.3f", myString, 4.0];


Not sure the extra temps buy you much.

robbieduncan
Sep 2, 2008, 04:06 AM
Have you read the documentation (http://developer.apple.com/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265)?

%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.


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.

anders94
Dec 16, 2009, 09:22 AM
If you wanted to use %s, you could get the UTF8String representation of myString:


NSString *result = [NSString stringWithFormat:@"%s?query=%.3f",[myString UTF8String],myValue];


That's a lower-case "s" there by the way.

Luke Redpath
Dec 16, 2009, 09:32 AM
If you wanted to use %s, you could get the UTF8String representation of myString:


NSString *result = [NSString stringWithFormat:@"%s?query=%.3f",[myString UTF8String],myValue];


That's a lower-case "s" there by the way.

Why would you do this?