Hot questions for Using Eureka in arrays

Question:

I have built a form(https://github.com/xmartlabs/Eureka) in my app and one of the input will obtain an Optional Set type. However,I need to push this set as an array to Parse for every user.But the array field in Parse only accepts NSArray as a valid data type.How do I convert it to an NSArray? I have already referred to this post (Convert native swift Set into array) but it does not seem to work in my case. Here's my code :

let user = PFUser()

print(form.rowByTag("Subjects")?.baseValue)

if let subjects = form.rowByTag("Subjects")?.baseValue
{
    let arr = Array(arrayLiteral: subjects)
    user["Subjects"] = arr as! AnyObject
}
else
{
    print("There is no subjects chosen")
}

The Subjects field is an array field in Parse. The result of the print statement and the error:


Answer:

The issue is that your objects inside the array are unwrapped optionals.

But you can't use Array(arrayLiteral: ) because it would just create an array containing the set itself, not an array of the elements in the set.

We are going to use flatMap instead, to safely unwrap the contents.

if let subjects = form.rowByTag("Subjects")?.baseValue as? Set<String> {
    let arr = subjects.flatMap { $0 }
    user["Subjects"] = arr as! AnyObject

}

Of course, if it's possible, it would be better to address the source of the issue and have the optionals safely unwrapped earlier in the code flow, that way you would be able to use Array(subjects) as usual.

Question:

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 :)


Answer:

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.