I finally got out of my problem (with the help of this forum in another thread, thanks), but I would like to clarify a point of Swift syntax vs objc.
In objc, it is possible (may be not desirable) for an init to create an instance which is of a subclass's type.
Here is an example taken from a tutorial :
DesktopEntity is aclass with 2 subclasses : DesktopImageEntity and DesktopFolderEntity
entityForURL can return either as one of the subclasses or nil
+ (DesktopEntity *)entityForURL:(NSURL *)url {
NSString *typeIdentifier;
if ([url getResourceValue:&typeIdentifier forKey:NSURLTypeIdentifierKey error:NULL]) {
NSArray *imgTypes = [NSImage imageTypes];
if ([imgTypes containsObject:typeIdentifier]) {
return [[DesktopImageEntity alloc] initWithFileURL:url];
} else if ([typeIdentifier isEqualToString:(NSString *)kUTTypeFolder]) {
return [[DesktopFolderEntity alloc] initWithFileURL:url];
}
}
return nil;
}this class method is called from an init in DesktopEntity
- (id)initWithPasteboardPropertyList:(id)propertyList ofType:(NSString *)type {
NSURL *url = [[NSURL alloc] initWithPasteboardPropertyList:propertyList ofType:type];
self = [DesktopEntity entityForURL:url];
return self;
}Checking in debugger shows that self is effectively of type DesktopImageEntity or DesktopFolderEntity
I have tried to achieve same thing in Swift, but could not find how.
init can return nil or DesktopEntity, but never a subtype.
However, compiler admits, in the init, the following syntax, but fails in execution of the downcast:
(self as? DesktopImageEntity)!.image = NSImage(byReferencingURL: url!)Am I missing something, or is it just impossible in Swift ? (I thought to use generics, but could not get it work).
So, I could confirm `initWithPasteboardPropertyList:ofType:` is called while executing `enumerateDraggingItemsWithOptions:forView:classes:searchOptions:usingBlock:`.
First, conformance to NSPasteboardReading is dynamically checked, then `+ readableTypesForPasteboard:`, `+ readingOptionsForType:pasteboard:`, and then `initWithPasteboardPropertyList:ofType:` is called. Some efforts finding workarounds (even if it contains unrecommended hacks) were futile...
Sorry for bothering and adding some noises to this thread. I really regret I was not trying to fully understand the discussion in the other thread.