类型 [Node] 不符合 Swift 4.0 中的 'Equatable'?
Type [Node] does not conform 'Equatable' in Swift 4.0?
我在我的项目中使用 Eureka,我想使用类型 [Node] 作为 Cell 的值类型:
final class TreeTVCell: Cell<[Node]>,CellType{//#1:compiling Error in Swift 4.0
}
大家知道,Cell的Value类型必须符合协议Equatable,而classNode是Objc写的,很简单:
#import "Node.h"
@implementation Node
- (instancetype)initWithParentId : (int)parentId nodeId : (int)nodeId name : (NSString *)name depth : (int)depth expand : (BOOL)expand{
self = [self init];
if (self) {
self.parentId = parentId;
self.nodeId = nodeId;
self.name = name;
self.depth = depth;
self.expand = expand;
}
return self;
}
@end
我的项目在 Swift 4.1 (Xcode 9.3.1) 中编译正常,但是如果我用 Xcode 9.2 (Swift 4.0) 打开项目,它将编译失败,抱怨:
Type '[Node]' does not conform to protocol 'Equatable'
我的问题是为什么它在 Swift 4.1 中编译正常而在 Swift 4.0 中编译失败?
以及如何在 Swift 4.0 中修复它?谢谢:)
在 Swift 4.1 之前,Equatable 数组本身不是 Equatable。这不是您可以直接在 4.0 中修复的问题;它缺乏条件一致性。您必须将 Array 包装在其他类型中(我们通常称其为 "boxing" 类型)并使其成为 Equatable。升级到 Swift 4.1.
有关详细信息,请参阅 Conditional Conformance in the Standard Library。
我在我的项目中使用 Eureka,我想使用类型 [Node] 作为 Cell 的值类型:
final class TreeTVCell: Cell<[Node]>,CellType{//#1:compiling Error in Swift 4.0
}
大家知道,Cell的Value类型必须符合协议Equatable,而classNode是Objc写的,很简单:
#import "Node.h"
@implementation Node
- (instancetype)initWithParentId : (int)parentId nodeId : (int)nodeId name : (NSString *)name depth : (int)depth expand : (BOOL)expand{
self = [self init];
if (self) {
self.parentId = parentId;
self.nodeId = nodeId;
self.name = name;
self.depth = depth;
self.expand = expand;
}
return self;
}
@end
我的项目在 Swift 4.1 (Xcode 9.3.1) 中编译正常,但是如果我用 Xcode 9.2 (Swift 4.0) 打开项目,它将编译失败,抱怨:
Type '[Node]' does not conform to protocol 'Equatable'
我的问题是为什么它在 Swift 4.1 中编译正常而在 Swift 4.0 中编译失败? 以及如何在 Swift 4.0 中修复它?谢谢:)
在 Swift 4.1 之前,Equatable 数组本身不是 Equatable。这不是您可以直接在 4.0 中修复的问题;它缺乏条件一致性。您必须将 Array 包装在其他类型中(我们通常称其为 "boxing" 类型)并使其成为 Equatable。升级到 Swift 4.1.
有关详细信息,请参阅 Conditional Conformance in the Standard Library。