Re: Unicode and various NSString questions ...
Re: Unicode and various NSString questions ...
- Subject: Re: Unicode and various NSString questions ...
- From: Andreas Mayer <email@hidden>
- Date: Mon, 2 Feb 2004 05:01:34 +0100
Am 02.02.2004 um 02:30 schrieb J Nozzi:
1. I'm assuming UTF-8 (as in stringWithUTF8String:) is Unicode. Is
this true?
Yes, UTF-8 is a Unicode encoding. See
http://www.unicode.org/versions/Unicode4.0.0/ch02.pdf
2. If I use NSString's "initWithContentsOfFile:", I assume whatever
encoding the text file was created with is what you get into the
NSString. Is this correct?
No. Read the docs:
--- quote ---
- (id)initWithContentsOfFile:(NSString *)path
Initializes the receiver, a newly allocated NSString, by reading
characters from the file named by path. If the contents begin with a
byte-order mark (U+FEFF or U+FFFE), interprets the contents as Unicode
characters; otherwise interprets the contents as characters in the
default C string encoding. Returns self, or nil if the file cant be
opened.
--- end quote ---
3. If I create an NSString based on the contents of an NSTextView,
what encoding IS it?
All internal strings are in unicode. But you don't need to know. The
encoding is relevant only, if you want to write the contents out. And
in that case you will normally specify the encoding you want.
4. Assuming the text file's contents were anything but Unicode, how
would I convert it?
You need to know what encoding it is, read the file into an NSData and
convert that to an NSString by using initWith
Data:encoding:
--- quote ---
- (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding
Initializes the receiver, a newly allocated NSString, by converting the
bytes in data into Unicode characters. data must be an NSData object
containing bytes in encoding and the default plain text format (that
is, pure content with no attributes or other markups) for that
encoding. Returns self.
--- end quote ---
bye. Andreas.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.