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 > Cocoa NSString question

Cocoa NSString question
Thread Tools
Dedicated MacNNer
Join Date: Dec 2003
Status: Offline
Reply With Quote
Jul 27, 2004, 08:50 PM
 
I am trying to do something with the Adium source, and came across this problem, that I cannot figure out. How can I typecast the ASCII code for a character in to a string (or character, or even a character array)? In C++, I could typecast it as (char)ASCII_Number, but in Objective-C, it doesn't seem to be doing it the same way.

To elaborate on what I am doing, I am trying to get Adium to recognize what a tab is, and I have tried both typecasting, and saying "\t" and '\t' and non if it works.
(Last edited by KraziKid; Jul 27, 2004 at 09:32 PM. )
15 inch MacBook Pro 2.16 GHz, 2 GB RAM, 7200 RPM 100GB HDD.

Dual 2.5 GHz Power Mac G5, 1 GB RAM, 250 GB HDD, ATI Radeon X800XT.
     
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Jul 27, 2004, 09:51 PM
 
Check the NSString method "stringWithCharacters:". Does this do what you require?

Otherwise, you should be able to type cast to a C string in the same way as in C (because Objective-C is merely an extension to C), and then use the NSString methos "stringWithCString:".
     
Dedicated MacNNer
Join Date: Dec 2003
Status: Offline
Reply With Quote
Jul 27, 2004, 11:42 PM
 
Originally posted by Brass:
Check the NSString method "stringWithCharacters:". Does this do what you require?

Otherwise, you should be able to type cast to a C string in the same way as in C (because Objective-C is merely an extension to C), and then use the NSString methos "stringWithCString:".
Thanks for that help. Right now, I have this code:
unichar chr[2];
chr[0]=011;
chr[1]=nil;
NSString *tabChar=[NSString stringWithCharacters:&chr length:1];

Is there any way to consolidate that in to one line? This does what I want, but I would prefer if I could get it down in to one line.
15 inch MacBook Pro 2.16 GHz, 2 GB RAM, 7200 RPM 100GB HDD.

Dual 2.5 GHz Power Mac G5, 1 GB RAM, 250 GB HDD, ATI Radeon X800XT.
     
Professional Poster
Join Date: Apr 2001
Location: Seattle, WA
Status: Offline
Reply With Quote
Jul 27, 2004, 11:47 PM
 
why not do this?
Code:
NSString *tab = @"\t";
The short shall inherit the earth. Just you wait. You won't see us coming. We'll pop out from under tables, beds, and closets in hordes. So you're tall, huh? You won't be so tall when I chew off your ankles. Mofo
     
Dedicated MacNNer
Join Date: Dec 2003
Status: Offline
Reply With Quote
Jul 27, 2004, 11:51 PM
 
Originally posted by cheerios:
why not do this?
Code:
NSString *tab = @"\t";
I have tried that, and it does not do what I require. I have no idea why, but it just does not.
15 inch MacBook Pro 2.16 GHz, 2 GB RAM, 7200 RPM 100GB HDD.

Dual 2.5 GHz Power Mac G5, 1 GB RAM, 250 GB HDD, ATI Radeon X800XT.
     
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
Jul 28, 2004, 12:36 AM
 
Originally posted by KraziKid:
Thanks for that help. Right now, I have this code:
unichar chr[2];
chr[0]=011;
chr[1]=nil;
NSString *tabChar=[NSString stringWithCharacters:&chr length:1];

Is there any way to consolidate that in to one line? This does what I want, but I would prefer if I could get it down in to one line.
Here's how I make an NSString from a unicode fraction character (all in one line):

NSString *threeQuarters = [NSString stringWithCharacters:(unichar *)"\x00\xbe" length:1];

However, if you're using plain ASCII characters, it's probably easier to simply do this:

NSString *theString = [NSString stringWithCString:"blahblahblah"];
     
Dedicated MacNNer
Join Date: Dec 2003
Status: Offline
Reply With Quote
Jul 28, 2004, 01:56 AM
 
Well, thanks for all the help. I ended up doing this in one line using this:
[NSString stringWithCString:"\t"]

For some reason, originally \t wasn't working, but when I did it using that method, it seemed to work now.
15 inch MacBook Pro 2.16 GHz, 2 GB RAM, 7200 RPM 100GB HDD.

Dual 2.5 GHz Power Mac G5, 1 GB RAM, 250 GB HDD, ATI Radeon X800XT.
     
   
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 01:06 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