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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
NSString from bool array BlobbleGame iPhone/iPad Programming 5 Jun 25, 2011 04:29 PM
"weird" Render stringWithFormat run-time error that crash the app. PizzaTray iPhone/iPad Programming 1 Nov 16, 2009 12:34 PM
Issue with stringWithFormat swimdev Mac Programming 2 Feb 2, 2009 02:03 PM
concatenate string stringWithFormat vidyareef iPhone/iPad Programming 4 Jan 20, 2009 10:32 AM
NSString not responding to stringWithFormat ! deamb iPhone/iPad Programming 3 Jul 6, 2008 08:14 AM


All times are GMT -5. The time now is 11:27 AM.

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

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