一个小上下文:我们使用CoreData,并且FolderEntity
和AlbumEntity
类是NodeEntity
的子类。如果我编译我们的代码进行调试或发布w /整个模块优化 OFF 事情按预期工作:
// fetch an entity from coreData, in this case it is a FolderEntity
// but reference it generically as a NodeEntity
if let node = context.optionalExistingObjectWithID(currentNodeID) as? NodeEntity {
print(node); // correctly says its a FolderEntity
print(node.contentTypes()); // correctly calls FolderEntity.contentTypes()
if let folder = node as? FolderEntity {
print(folder.contentTypes()); // correctly calls FolderEntity.contentTypes()
}
如果我将打开整个模块优化并编译为调试,事情也会如上所述。但是,如果我转 ON 整个模块优化并编译发布多态性变得混乱
// fetch an entity from coreData, in this case it is a FolderEntity
// but reference it generically as a NodeEntity
if let node = context.optionalExistingObjectWithID(currentNodeID) as? NodeEntity {
print(node); // correctly says its a FolderEntity
print(node.contentTypes()); // incorrectly calls the superclass' NodeEntity.contentTypes()
if let folder = node as? FolderEntity {
print(folder.contentTypes()); // correctly calls FolderEntity.contentTypes()
}
NodeEntity
(超类)将contentTypes()定义为
func contentTypes() -> [SMContentType] {
return [];
}
FolderEntity
(子类)将contentTypes()定义为
override func contentTypes() -> [SMContentType] {
return [ SMContentType.Folder, SMContentType.Album ];
}
为什么WMO会像这样搞乱继承?
答案 0 :(得分:2)
这是一个错误。提交错误报告。您的错误可能在Xcode 7.1中得到修复,因此请在提交前尝试(目前处于测试阶段):
使用as导致动态协议一致性检查的问题?整个优化模式失败的协议已得到修复
但如果没有修复肯定会提交。发布版本不同的行为是一个错误,简单明了。你不应该做任何特别的事情来解决它。