Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-23-2009, 06:53 PM   #1 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default stringWithFormat with varying number of arguments

I'm wondering the best way to approach the following problem,

My app downloads from a website a string with looks something like this:
Code:
"I have 3 parameters. The first is %@, the second is %@ and the third is %@"
I know I can using stringWithFormat like this to pass three parameters:
Code:
NSString * downloadedString = @"I have 3 parameters. The first is %@, the second is %@ and the third is %@";
NSString * formattedString = [NSString stringWithFormat:downloadedString,firstValue,secondValue,thirdValue];
What I'm not sure how to do is pass a varying number of parameters/arguments to stringWithFormat.

Let's say for instance that the string that's being downloaded changed to:
Code:
"4 params. 1 = %@, 2 = %@, 3 = %@ and 4 = %@"
Instead of rewriting the code, it would be good if the app could just deal with it, working out there is 4 arguments this time, and passing 4 to it.

I can tell the app how many arguments there are going to be, I can also tell the app what those arguments should be, what I'm uncertain on is how code for the changes.

Something like:
Code:
NSArray * arrayContainingArguments = [[NSArray alloc] initWithObjects:@"first",@"second",@"third",@"forth",nil];
NSString * downloadedString = @"4 params. 1 = %@, 2 = %@, 3 = %@ and 4 = %@";
NSString * finalString = [NSString stringWithFormat:downloadedString,arrayContainingArguments];
[arrayContainingArguments release];
Any ideas?

Tom.
harrytheshark is offline   Reply With Quote
Old 11-23-2009, 07:06 PM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

I think you'll have to write your own method for this that takes an array and returns a string.

Your method could create a new NSMutableString, loop through the array calling appendString or appendFormat, and then returning the final string.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 11-23-2009, 07:18 PM   #3 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

Thanks for the reply and idea.
I managed to stumble across initWithFormat:arguments in the documentation and have got something working from that.

Thanks again,
Tom.
harrytheshark is offline   Reply With Quote
Old 07-09-2010, 05:07 AM   #4 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 2
kenji is on a distinguished road
Default How did you do it?

Hi could you post some code how you did it?
I'm trying to do this kind of string formatting too.
kenji is offline   Reply With Quote
Old 07-09-2010, 05:12 AM   #5 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 2
kenji is on a distinguished road
Default

Nevermind, found another post with a sample. Anyways, thanks for the hint

How to "pass on" a variable number of arguments to NSString's +stringWithFormat: - Stack Overflow
kenji is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
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
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 334
13 members and 321 guests
akacaj, alexP, ClerurcifeDer, Duncan C, givensur, GraffitiCircus, guusleijsten, JmayLive, NetGuru, Paul Slocum, Punkjumper, Sloshmonster, yys
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,114
Posts: 402,883
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:45 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0