I have the following code in a tutorial :
@implementation DesktopEntity
- (id)initWithFileURL:(NSURL *)fileURL {
self = [super init];
if (self) {
_fileURL = fileURL;
}
return self;
}
+ (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;
}
- (id)initWithPasteboardPropertyList:(id)propertyList ofType:(NSString *)type {
NSURL *url = [[NSURL alloc] initWithPasteboardPropertyList:propertyList ofType:type];
self = [DesktopEntity entityForURL:url];
return self;
}, I am not sure how to convert to swift the initWithPasteboardPropertyList
If I call the class function DesktopEntity entityForURL:url, to which object should I assign its result ? Obviously I cannot write
self = DesktopEntity.entityForURL(url!)here is what I wrote so far
class DesktopEntity: NSObject, NSPasteboardWriting, NSPasteboardReading {
var fileUrl : NSURL
private var _name : String /
init(fileURL: NSURL) {
self.fileUrl = fileURL
_name = ""
super.init()
}
class func entityForURL(url: NSURL) -> DesktopEntity? {
var typeIdentifier: AnyObject? /
do {
try url.getResourceValue(&typeIdentifier, forKey: NSURLTypeIdentifierKey) /
let imgTypes = NSImage.imageTypes()
let stringTypeIdentifier = typeIdentifier as! String
if imgTypes.contains(stringTypeIdentifier) {
return DesktopImageEntity(fileURL: url)
} else if stringTypeIdentifier == kUTTypeFolder as String {
return DesktopFolderEntity(fileURL: url)
}
} catch _ {}
return nil /
}
var name : String? {
get {
var localName: AnyObject?
do {
try fileUrl.getResourceValue(&localName, forKey: NSURLLocalizedNameKey)
return localName as? String
} catch _ { }
return _name /
}
set {
_name = newValue!
}
}
required init!(pasteboardPropertyList propertyList: AnyObject, ofType type: String) {
let url = NSURL(pasteboardPropertyList: propertyList, ofType: type)
// How to implement : DesktopEntity.entityForURL(url!)
self.fileUrl = url!
_name = ""
super.init()
}