Re: Throwing an error from a NSDocument.init override
Re: Throwing an error from a NSDocument.init override
- Subject: Re: Throwing an error from a NSDocument.init override
- From: Daryle Walker <email@hidden>
- Date: Wed, 15 Feb 2017 03:29:38 -0500
> On Feb 14, 2017, at 1:24 PM, Quincey Morris <email@hidden> wrote:
>
> On Feb 14, 2017, at 00:26 , Daryle Walker <email@hidden <mailto:email@hidden>> wrote:
>>
>> But I can’t call the “super” versions of those other initializers from within my override.
>
> This is the essence of your problem. Your “override” cannot be a convenience initializer because convenience initializers cannot call “up” to the superclass, and it cannot be a designated initializer because designated initializers cannot call convenience initializers. For one solution, see here:
>
> http://stackoverflow.com/questions/38908902/overwriting-nsdocuments-initcontentsofoftype-in-swift <http://stackoverflow.com/questions/38908902/overwriting-nsdocuments-initcontentsofoftype-in-swift>
>
> Answering the same question in another way, look at the comments for init(type:) in the NSDocument header file:
>
>> "Initialize a new empty document of a specified type, and return it if successful. If not successful, return nil after setting *outError to an NSError that encapsulates the reason why the document could not be initialized. The default implementation of this method just invokes [self init] and [self setFileType:typeName].”
>
>
> This tells you exactly what the superclass convenience initializer does, and the fact that it’s in the header file makes the behavior a public API contract. So, you are allowed to implement your own subclass convenience initializer (that throws) which does *not* invoke super, but simply does what the super method is documented to do. (That’s basically the solution given on Stack Overflow.)
This is what I did; repeat the three initializers. Apple’s code will call my versions instead of the ones in NSDocument, right?
The given notes don’t say how to get the modification date, but I got it from the StackOverflow article you mentioned. For the initializer that can take a “URL?” for a decoy file, which modification date are you supposed to use? My code uses “(urlOrNil ?? contentsURL)” to decide. Is that right?
—
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT mac DOT com
_______________________________________________
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