Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Sep 2, 2008, 04:00 AM   #1
RAMilewski
macrumors newbie
 
Join Date: Sep 2006
Location: Northern California
NSString stringWithFormat woes

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?

Last edited by kainjow; Dec 16, 2009 at 01:50 PM. Reason: code
RAMilewski is offline   0 Reply With Quote
Old Sep 2, 2008, 04:04 AM   #2
Luke Redpath
macrumors 6502a
 
Join Date: Nov 2007
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.
Luke Redpath is offline   0 Reply With Quote
Old Sep 2, 2008, 04:06 AM   #3
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
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.

Last edited by jsw; Sep 2, 2008 at 10:38 AM. Reason: Toned down a bit
robbieduncan is offline   0 Reply With Quote
Old Dec 16, 2009, 09:22 AM   #4
anders94
macrumors newbie
 
Join Date: Dec 2009
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.
anders94 is offline   0 Reply With Quote
Old Dec 16, 2009, 09:32 AM   #5
Luke Redpath
macrumors 6502a
 
Join Date: Nov 2007
Quote:
Originally Posted by anders94 View Post
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.
Why would you do this?
Luke Redpath is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
C Function from NSString? ArtOfWarfare Mac Programming 20 Jun 16, 2013 10:55 AM
NSString to NSSize larswik Mac Programming 1 May 28, 2013 06:54 PM
NSString and NSRange Tutorials nashyo iPhone/iPad Programming 3 Aug 24, 2012 09:08 PM
stringWithFormat within a For loop animefx iPhone/iPad Programming 2 Jul 17, 2012 09:45 AM
(NSString*) ?? thedon1 iPhone/iPad Programming 6 Jul 2, 2012 07:52 PM

Forum Jump

All times are GMT -5. The time now is 02:37 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC