Search code examples
arraysswifteureka-forms

Type [Node] does not conform 'Equatable' in Swift 4.0?


I use Eureka in my Project,and I want use type [Node] for Cell's Value type:

final class TreeTVCell: Cell<[Node]>,CellType{//#1:compiling Error in Swift 4.0

}

As you know , Cell's Value type must conform protocol Equatable,and class Node wrote by Objc,it's very simple:

#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

My project compiled OK in Swift 4.1 (Xcode 9.3.1),But if I open project with Xcode 9.2 (Swift 4.0),it will be comiled failed, it's complain that :

Type '[Node]' does not conform to protocol 'Equatable'

my Q is why it's compiled ok in Swift 4.1 and failed in Swift 4.0? and How to fix it in Swift 4.0? Thanks :)


Solution

  • Prior to Swift 4.1, Arrays of Equatable are not themselves Equatable. This isn't something you can fix in 4.0 directly; it lacks conditional conformances. You would have to wrap the Array in some other type (we usually call it "boxing" the type) and make that Equatable. Upgrade to Swift 4.1.

    See Conditional Conformance in the Standard Library for more details.