Swift 3 从 [NSManagedObject] 转换为不相关的类型 LeadSource 总是失败
Swift 3 Cast from [NSManagedObject] to unrelated type LeadSource always fails
这条线被抛出警告:
LeadSource.mr_findAll(in: localContext).flatMap({[=11=] as? LeadSource}).forEach({[=11=].MR_deleteEntityInContext(localContext)})
所以 LeadSource class 是 _LeadSource 的子class,而 _LeadSource 是 NSManagedObject 的子class,这意味着我应该能够将它转换为 LeadSource。 mr_findAll 函数位于 NSManagedObject+MagicalFinders.h 文件中,它是 NSManagedObject 的扩展,这意味着它首先将 LeadSource 识别为 NSManagedObject。为什么会抛出此警告?
LeadSource.h
@interface LeadSource : _LeadSource
_LeadSource.h
@interface _LeadSource : NSManagedObject
mr_findAll returns一个NSManagedObject,而LeadSource是NSManagedObject的一个子class,那为什么说转换失败呢?
来自评论:
"The second crucial information besides the classes is that a cast from an array to a non-array always fails."
正在将我的代码更改为...{$0 as? [LeadSource]}...解决了我的问题,请注意 LeadSource 周围的括号将其转换为数组。
这条线被抛出警告:
LeadSource.mr_findAll(in: localContext).flatMap({[=11=] as? LeadSource}).forEach({[=11=].MR_deleteEntityInContext(localContext)})
所以 LeadSource class 是 _LeadSource 的子class,而 _LeadSource 是 NSManagedObject 的子class,这意味着我应该能够将它转换为 LeadSource。 mr_findAll 函数位于 NSManagedObject+MagicalFinders.h 文件中,它是 NSManagedObject 的扩展,这意味着它首先将 LeadSource 识别为 NSManagedObject。为什么会抛出此警告?
LeadSource.h
@interface LeadSource : _LeadSource
_LeadSource.h
@interface _LeadSource : NSManagedObject
mr_findAll returns一个NSManagedObject,而LeadSource是NSManagedObject的一个子class,那为什么说转换失败呢?
来自评论:
"The second crucial information besides the classes is that a cast from an array to a non-array always fails."
正在将我的代码更改为...{$0 as? [LeadSource]}...解决了我的问题,请注意 LeadSource 周围的括号将其转换为数组。