How to combine multiple nullable NSPredicates?

半腔热情 提交于 2019-12-12 18:18:24

问题


For eg, Something like:

    var finalPredicate = NSPredicate(format: "")

    if (screen != nil) {
        screenPredicate = NSPredicate(format: "screen = %@", screen!)
        finalPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [screenPredicate!])
    }

    if (feature != nil) {
        featurePredicate = NSPredicate(format: "feature = %@", feature!)
        finalPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [finalPredicate, featurePredicate!])
    }

    if (shouldDisplayPredicate != nil) {
        shouldDisplayPredicate = NSPredicate(format: "shouldDisplay = %@", shouldDisplay!)
        finalPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [finalPredicate, shouldDisplayPredicate!])
    }
    if (hasDisplayed != nil) {
        displayPredicate = NSPredicate(format: "hasDisplayed = %@", hasDisplayed!)
        finalPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [finalPredicate, displayPredicate!])
    }

Is there a better way to do this where the predicates could be null or not ?

Thanks


回答1:


First, you should avoid the forced unwrapping and replace

if screen != nil {
    // ... add predicate for `screen!` ...
}

by an optional binding:

if let screenValue = screen {
    // ... add predicate for `screenValue` ...
}

Compare When should I compare an optional value to nil? for a good overview of that topic. The same can be achieved more compactly using the map() method of Optional:

screen.map { /* ... add predicate for `$0` ... }

The closure is called only if screen != nil, and then $0 inside the closure is the unwrapped value.

Second, it is simpler to fill an array with all required predicates first, and create the compound predicate only once. This also allows you to check if any search attribute was set at all.

Your code then becomes

var predicates: [NSPredicate] = []
if let screenValue = screen {
    predicates.append(NSPredicate(format: "screen = %@", screenValue))
}
if let featureValue = feature {
    predicates.append(NSPredicate(format: "feature = %@", featureValue))
}
// ... other search attributes ...
if !predicates.isEmpty {
    let finalPredicate = NSCompoundPredicate(andPredicateWithSubpredicates:predicates)
}

or

var predicates: [NSPredicate] = []
screen.map { predicates.append(NSPredicate(format: "screen = %@", $0)) }
feature.map { predicates.append(NSPredicate(format: "feature = %@", $0)) }
// ... other search attributes ...
if !predicates.isEmpty {
    let finalPredicate = NSCompoundPredicate(andPredicateWithSubpredicates:predicates)
}


来源:https://stackoverflow.com/questions/39027558/how-to-combine-multiple-nullable-nspredicates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!