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 > NSData from NSString Question

NSData from NSString Question
Thread Tools
titan44
Fresh-Faced Recruit
Join Date: Sep 2003
Location: Victoria, Australia
Status: Offline
Reply With Quote
May 14, 2006, 07:57 PM
 
I'm having a few problems converting a NSString to a NSData type.

doing NSData = NSString;, does the job, but generated a warning "assignment from distinct Objective - c type", but dumping to NSLog shows the output look to be correct. I've tried using various withEncoding types but end up with rubbish, the NSSting contains a file name with extension i need to write to.

Any ideas on the best way to accomplish this?
     
Brass
Professional Poster
Join Date: Nov 2000
Location: Tasmania, Australia
Status: Offline
Reply With Quote
May 14, 2006, 09:45 PM
 
NSData *dataObject = [aString dataUsingEncoding:NSUnicodeStringEncoding];

(or substitute any of the list below for "NSUnicodeStringEncoding" depending on the encoding of your string)

Is this what you've tried?

Other encoding types (from the documentation)

enum {
NSASCIIStringEncoding = 1,
NSNEXTSTEPStringEncoding = 2,
NSJapaneseEUCStringEncoding = 3,
NSUTF8StringEncoding = 4,
NSISOLatin1StringEncoding = 5,
NSSymbolStringEncoding = 6,
NSNonLossyASCIIStringEncoding = 7,
NSShiftJISStringEncoding = 8,
NSISOLatin2StringEncoding = 9,
NSUnicodeStringEncoding = 10,
NSWindowsCP1251StringEncoding = 11,
NSWindowsCP1252StringEncoding = 12,
NSWindowsCP1253StringEncoding = 13,
NSWindowsCP1254StringEncoding = 14,
NSWindowsCP1250StringEncoding = 15,
NSISO2022JPStringEncoding = 21,
NSMacOSRomanStringEncoding = 30,
NSProprietaryStringEncoding = 65536
};
     
Wevah
Senior User
Join Date: Nov 2001
Location: State of Denial
Status: Offline
Reply With Quote
May 14, 2006, 09:48 PM
 
but dumping to NSLog shows the output look to be correct

The only reason this "works" is that it's still an NSString, not an NSData object.
[Wevah setPostCount:[Wevah postCount] + 1];
     
titan44  (op)
Fresh-Faced Recruit
Join Date: Sep 2003
Location: Victoria, Australia
Status: Offline
Reply With Quote
May 14, 2006, 10:04 PM
 
AHH, o.k thanks guys, i'll give that a shot
     
   
 
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
Top
Privacy Policy
All times are GMT -4. The time now is 12:46 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,