Re: NSDocument window opens - even if the document didn't read properly.
Re: NSDocument window opens - even if the document didn't read properly.
- Subject: Re: NSDocument window opens - even if the document didn't read properly.
- From: Mike Abdullah <email@hidden>
- Date: Sun, 29 Jul 2012 19:10:28 +0100
Hello, there are many things wrong with your code. I’m noting them below.
On 29 Jul 2012, at 18:53, Pascal Harris wrote:
> I hope that someone here might be able to help me with a couple of queries.
>
> 1. I'm trying to open a document (NSDocument). If the file is good then my program opens it without problem. If the document fails to parse correctly then the NSDocument window still opens - but it opens empty. Of course, if parsing of the document fails then I want the document window not to open at all. The document data gets read as so and if it fails then I return NO - the document window still opens though. Can anyone suggest what I might have missed or messed up?
>
> - (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
> {
> BOOL success = NO;
>
> success = [self loadTextViewWithInitialData: data];
Uh-oh, if this call failed, you’re returning NO without filling in the error pointer. This will either blow up or provide a poor message to the user. Hopefully your -loadTextView… method can report why it failed.
> if (!success)
> {
> NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
Cocoa has URL-based methods for this in NSFileManager these days; you should consider adopting them.
> NSString* URLString = [[[paths objectAtIndex:0] stringByAppendingString:[self fileURL].lastPathComponent]stringByAppendingString:@".bad"];
This is some seriously mangled code. Cocoa provides path manipulation-specific string routines. USE THEM.
> NSURL* destinationURL = [NSURL URLWithString:[URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
This is NOT how you convert from a path to a URL. Use +fileURLWithPath: instead.
> BOOL success = [[NSFileManager defaultManager] copyItemAtURL:[self fileURL] toURL:destinationURL error:nil];
You’ve just stored success into a *new* variable. I’m guessing you wanted to store it into the existing success variable. Unless you’ve changed the default compiler settings, the compiler should be warning you here that the success variable is unused. Compiler warnings are useful. HEED THEM.
> }
>
> return success;
> }
_______________________________________________
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