2

Back in Xcode 6, in a project using Core Data I had the following line. It worked fine.

fetchRequest.predicate = NSCompoundPredicate(type: .AndPredicateType, subpredicates: [predicate, datePredicate])

predicate and datePredicate are of NSPredicate type.

Yesterday I updated to Xcode 6.1 and now this above line gives this error Could not find member 'AndPredicateType'. Specifying the entire value like this NSCompoundPredicateType.AndPredicateType didn't work either.

Then I changed the line to use its convenience method line below.

fetchRequest.predicate = NSCompoundPredicate.andPredicateWithSubpredicates([predicate, datePredicate])

Now I get a new error Cannot convert the expression's type '()' to type 'NSPredicate?'. I don't understand why. The documentation doesn't show any deprecations or changes to NSCompoundPredicate either.

Can anyone please tell me how to correct this?

Thank you.

4

1 回答 1

7

初始化器

extension NSPredicate {
    convenience init?(format predicateFormat: String, _ args: CVarArgType...)
}

现在是一个失败的初始化器。它返回一个可选的NSPredicate?,您必须解包结果(或使用可选绑定)。例如:

let compoundPredicate = NSCompoundPredicate(type: .AndPredicateType, subpredicates: [predicate!, datePredicate!])
// Or:
let compoundPredicate = NSCompoundPredicate.andPredicateWithSubpredicates([predicate!, datePredicate!])

参考:

于 2014-10-21T06:19:29.863 回答