Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Objective-C: Putting multiple strings together into one var for printing

Objective-C: Putting multiple strings together into one var for printing
Thread Tools
Xeo
Moderator Emeritus
Join Date: Mar 2001
Location: Austin, MN, USA
Status: Offline
Reply With Quote
Nov 27, 2002, 12:37 AM
 
In PHP when I have multiple variables, I can put them all together so when I print the same text several times throughout the code it's always the same. Something like:
Code:
$var1 = "Hi"; $var2 = "What's Up?"; $var3 = $var1.$var2; echo $var3; // produces "HiWhat's Up?"
I want to do something similar in Objective-C but I can't figure it out. Here is what I have:
Code:
NSString * lastName = [anObject valueForProperty:kABLastNameProperty]; NSString * firstName = [anObject valueForProperty:kABFirstNameProperty]; printf("$addrBook[\"%s%s%d\"]", [lastName UTF8String], [firstName UTF8String], arrIndex);
arrIndex is an integer, lastName and firstName are NSStrings. I want to all the text into a variable once, and just print out that when I need it. Right now, I'm reusing the printf() line multiple times so it's always parsing through. If I could just set the string and print it, it'd be better.

Second problem, how do I change that integer into a string. I was forced to use %d because I couldn't for the life of me find a function that would convert the integer to a string. I tried [arrIndex stringValue] but that didn't work. Any ideas?

Thanks in advance. I'll keep posting my problems as I come across them.
     
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Nov 27, 2002, 12:45 AM
 
Code:
NSString *compositeString = [NSString stringWithFormat: @"$addrBook[\"%@%@%d\"]", lastName, firstName, arrIndex]; NSLog(compositeString); // to print it to the console
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Nov 27, 2002, 05:00 AM
 
Hi,

Your format is wrong: It is correct to put:

Code:
NSLog(@"this is a C string: %s", someCString);
however you are using NSStrings - which are fully fledged objects: therefore the format should be:

Code:
NSLog(@"this is an NSString: %@ ", someNSString);
(notice the @ symbol)

If you want to combine strings you cannot dot them - like in PHP - you have to use a method of NSString to combine them - have a look at the Instance Methods of NSString:

file:///System/Library/Frameworks/Fo.../NSString.html
     
   
Thread Tools
Forum Links
Forum 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
Top
Privacy Policy
All times are GMT -5. The time now is 06:24 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2