简体   繁体   中英

Swift 3 Cast from [NSManagedObject] to unrelated type LeadSource always fails

That warning is being thrown for this line:

LeadSource.mr_findAll(in: localContext).flatMap({$0 as? LeadSource}).forEach({$0.MR_deleteEntityInContext(localContext)})

So the LeadSource class is a subclass of _LeadSource which is a subclass of NSManagedObject, which means that I should be able to cast it to a LeadSource. the mr_findAll function is located in the NSManagedObject+MagicalFinders.h file, which is an extension of NSManagedObject, which means that it recognizes the LeadSource as a NSManagedObject in the first place. Why is this warning being thrown?

LeadSource.h
@interface LeadSource : _LeadSource

_LeadSource.h
@interface _LeadSource : NSManagedObject

mr_findAll returns a NSManagedObject, and LeadSource is a subclass of NSManagedObject, so why does it say the cast fails?

From the comments:

"The second crucial information besides the classes is that a cast from an array to a non-array always fails."

Changing my code to ...{$0 as? [LeadSource]}... fixed my issue, notice the brackets around LeadSource casting it to an array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM