Re: Tiger Decoding problem
Re: Tiger Decoding problem
- Subject: Re: Tiger Decoding problem
- From: Ondra Cada <email@hidden>
- Date: Mon, 30 May 2005 03:46:47 +0200
Nicholas,
well, it is all documented :), but it's always a pleasure to help a
newbie :)
On 30.5.2005, at 2:24, Nicholas Crosbie wrote:
What's wrong with my code (see below)?
#import <Cocoa/Cocoa.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
NSString *path = [NSString new];
No need to create a new object...
path = @"/Users/nicholas/test2/fileB";
... to be immediately replaced by another. Also, with new it would
leak (none of the errors so far is fatal).
NSString *contents = [NSString
stringWithContentsOfFile:path NSStringEncoding
The "NSStringEncoding" above is the culprit it cannot be built. It
just does not belong there. It is the argument type.
encoding:NSUTF8StringEncoding];
Here you lack the error: argument.
NSLog(@"contents %@",contents);
[pool release];
In this special case, not needed (but some would probably argue its
cleaner from the theoretical point of view :))
return 0;
}
All right, here is how it works:
56 /tmp> <q.m
#import <Cocoa/Cocoa.h>
int main() {
[NSAutoreleasePool new];
NSError *err; // no need to create/initialize any object, just
declaring a variable
NSString *contents=[NSString stringWithContentsOfFile:@"/tmp/q"
encoding:NSUTF8StringEncoding error:&err];
// note '&' above: the err variable is references, so that the
method can change its value
if (!contents) // could not read in the contents for some reason: let
us see the reason
NSLog(@"ERROR: %@",err); // which was filled into err by the method
else /*all right*/ NSLog(@"OK: %@",contents);
return 0;
}
57 /tmp> cc -Wall -framework Cocoa q.m
58 /tmp> ./a.out
2005-05-30 03:40:36.851 a.out[2287] ERROR: NSError "File “q” does
not exist." Domain=NSCocoaErrorDomain Code=260 UserInfo={
NSFilePath = "/tmp/q";
NSUnderlyingError = NSError "POSIX error: No such file or
directory" Domain=NSPOSIXErrorDomain Code=2;
}
59 /tmp> touch q
60 /tmp> ./a.out
2005-05-30 03:40:46.810 a.out[2289] OK:
61 /tmp> echo 'Whatever' > q
62 /tmp> ./a.out
2005-05-30 03:41:02.233 a.out[2290] OK: Whatever
63 /tmp> # from TextEdit, saved into q a UTF-8 nonsense
63 /tmp> ./a.out
2005-05-30 03:45:12.471 a.out[2308] ERROR: NSError "File “q” could
not be opened using text encoding Unicode (UTF-8)."
Domain=NSCocoaErrorDomain Code=261 UserInfo={NSFilePath = "/tmp/q";
NSStringEncoding = 4; }
64 /tmp>
All right?
---
Ondra Čada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
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