Re: Two Questions About Parsing a String
Re: Two Questions About Parsing a String
- Subject: Re: Two Questions About Parsing a String
- From: PGM <email@hidden>
- Date: Tue, 22 Jul 2008 12:29:15 +0200
On 22-Jul-08, at 11:09 AM, Ian Jackson wrote:
I'm looking for some help parsing a string from a file.
Firstly, getting the string is causing some issues. I read the
String Programming Guide for Cocoa, and got this:
NSString *path = ...;
NSData *data = [NSData dataWithContentsOfFile:path];
// assuming data is in UTF8
NSString *string = [NSString stringWithUTF8String:[data bytes]];
along with a warning that you must not use:
stringWithContentsOfFile:
So, I tried to do as I was told, but using the first example, I find
that successfully getting a string from the data is quite random.
When I start the application, my first attempt to load the file may
or may not result and a string being created, but if not, repeatedly
trying to load the file eventually works and I get the string. I'm
not sure what the encoding is, (I'm trying to create a .obj reader),
but setting it at UTF8 in Xcode doesn't help.
So, I took a peek at the dark side, and tried the soon to be
deprecated stringWithContentsOfFile: which works fine all the time.
Any thoughts on why the first example might be failing randomly?
Have you tried the non-deprecated
stringWithContentsOfFile:usedEncoding:error: or
stringWithContentsOfFile:encoding:error: ? The former actually attemps
to determine the encoding used for the file and returns that by
reference. They also allow error handling, so you can determine why
your files may not be read successfully.
Cheers, Patrick
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden