Swift generics and extensions functions extend

Having strange problem with generics in Swift

have a subclass hierarchy NSManagedObject

with extensions
:

BaseCloudKitItem <-MyObject

extension BaseCloudKitItem {
    func updateDataWithCKRecord(record: CKRecord!, inManagedObjectContext context: NSManagedObjectContext!) {
        cloudKitRecordID = record?.recordID.recordName
        cloudKitType = record?.recordType
        println("\(self) updateDataWithCKRecord:inManagedObjectContext: called")
    }
}

extension MyObject {
    override func updateDataWithCKRecord(record: CKRecord!, inManagedObjectContext context: NSManagedObjectContext!) {
        super.updateDataWithCKRecord(record, inManagedObjectContext: context)
        title = record?.objectForKey("title") as? NSString
    }
}

      

have a common class:

class CloudFetcher<T : BaseCloudKitItem> {
    class func someFetchingAndMappingMethod(completion:(() -> Void)?) {
         someFetchingMethodWithCompletion { (records, error) in
             if let err = error {
                 completion?(nil, err)
             } else {
                 NSManagedObjectContext.saveDataInBackgroundWithBlock({ (localContext) in
                     T.deleteAllInContext(localContext)
                     for record in records {
                         let object = T.createEntityInContext(localContext) as T
                         object.updateDataWithCKRecord(record, inManagedObjectContext: localContext)
                     }
                     }, completion: completion)
                 }
             }
        }
    }

      

and use:

CloudFetcher<MyObject>.someFetchingAndMappingMethod { _ in

}

      

The passed class to the general - CloudFetcher

MyObject

So, the problem is that I found that methods called internally generic from the base class and not from . If I change the declaration of the CloudFetcher interface from to , it works great, but it doesn't make sense to use generics this way. BaseCloudKitItem

MyObject

class CloudFetcher<T : BaseCloudKitItem>

class CloudFetcher<T : MyObject>

Also, in the logs from println()

inside the target method, I see the class I want, but the required method is still not called:

<MyObject: 0x7aab7130> (entity: MyObject; id: 0x802f5bf0 <x-coredata:///InstrumentToken/t1AAF2438-9DF7-44DA-89B2-C3C1BE3D91FE17> ; data: {
    cloudKitRecordID = "fb4fac88-40e5-4aef-af3c-6e36867dbf5f";
    cloudKitType = MyObject;
    title = nil;
}) updateDataWithCKRecord:inManagedObjectContext: called

      

Thats looks pretty strange to me, maybe someone can suggest me how I could solve it?

Thank!

+3


source to share





All Articles