Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

RAMilewski

macrumors newbie
Original poster
Sep 25, 2006
4
0
Northern California
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?
 

Luke Redpath

macrumors 6502a
Nov 9, 2007
733
6
Colchester, UK
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.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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.
 

anders94

macrumors newbie
Dec 16, 2009
1
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.