Combining Two Conditions in NSPredicate

烂漫一生 提交于 2019-11-30 00:36:38

问题


How do you combine two conditions in NSPredicate? I am using the following statement and I would like to add another condition that compares the the password with the contents of a textfield using AND:

request.predicate = NSPredicate(format: "username = %@", txtUserName.text!)

回答1:


Try this

request.predicate = NSPredicate(format: "username = %@ AND password = %@", txtUserName.text!, txtPassword.text!)



回答2:


As already said, you can use logical operators like "AND", "OR" in predicates. Details can be found in Predicate Format String Syntax in the "Predicate Programming Guide".

As an alternative, use "compound predicates":

let p1 = NSPredicate(format: "username = %@", "user")
let p2 = NSPredicate(format: "password = %@", "password")
let predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [p1, p2])

This is useful for more complex expressions, or if you want to build a predicate dynamically at runtime.




回答3:


AND is exactly what you need

request.predicate = NSPredicate(format: "username = %@ AND password = %@", txtUserName.text!, txtPassWord.text!)


来源:https://stackoverflow.com/questions/33321050/combining-two-conditions-in-nspredicate

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