Re: Cocoa Books
Re: Cocoa Books
- Subject: Re: Cocoa Books
- From: Ondra Cada <email@hidden>
- Date: Tue, 18 Apr 2006 01:25:01 +0200
Phil,
Actually, I really want to check BYTES as you suggest. I thought I
had to start with an NSString and convert it to an ASCII code. ...
OK. So now I need to investigate NSData.
Easier than NSStrings, for you don't need to mess up with them
encodings. Just
NSError *err; // this takes from you the complete burden of...
NSData *d=[NSData dataWithContentsOfFile:yourfilename options:0
error:&err];
if (!d) [NSApp presentError:err]; // ... reporting possible reading
errors
else {
unsigned char *bytes=[d bytes];
// you SHOULD check [d length] here if long enough!
[statusText setIntValue:bytes[8]];
...
}
Onwards & upwards... [I've got a headache!]
Been there myself :) Do scan over the complete Foundation and AppKit
documentation at least once: not reading all the text of course, just
checking the class names and the method lists "by task". It takes a
day or so, but from the moment on, you'll always have a rough idea
what is supported and where to search for the support.
And by the way, do not believe all you read here :) -- if, in near
future, you need to work with strings again, then...
On 18.4.2006, at 0:47, Nir Soffer wrote:
- Opening a text file and reading from it
If your text file is plain ASCII or UTF-16:
[NSString stringWithContentsOfFile:@"/path/to/file"]
Don't. Although it would happen to work, still
stringWithContentsOfFile: is deprecated (and for good reasons too).
Instead, use stringWithContentsOfFile:encoding:error: with the proper
encoding (NSASCIIStringEncoding, NSUnicodeStringEncoding).
Incidentally: always check whether there is a method with the error:
argument for whatever you are trying to do, and if so, use it (and
ignore the others, which tend to be older ones kept for compatibility
only). It allows you to present any possible fault to the user
without any effort (in a way similar to my code excerpt above: there
are more ways to present an error, but for start, just sending it to
NSApp suffices).
If you use some other encoding, you must read the file with the
correct encoding. First get the file data:
NSData *data = [NSData dataWithContentsOfFile:@"/path/to/file"];
Then create a string from the data using the correct encoding. For
UTF-8 you can use:
NSString *string = [NSString stringWithUTF8String:[data bytes]];
For other encodings, e.g. ISO-8859-1:
NSString *string = [[NSString alloc] initWithData:data encoding:
NSISOLatin1StringEncoding];
[string autorelease];
Would work, but is an unnecessary hassle. Again, you can use directly
stringWithContentsOfFile:encoding:error: with the desired encoding.
Almost any encoding used is supported; you can use the following code
to list them all:
const NSStringEncoding *ec=[NSString availableStringEncodings];
for (;*ec;ec++) NSLog(@"%@: %d",[NSString
localizedNameOfStringEncoding:*ec],*ec);
Note: you probably do *not* want to use
stringWithContentsOfFile:usedEncoding:error: -- theoretically, it
would determine the encoding actually used for you; in practice it is
not too good determining though.
---
Ondra Čada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden