和/或与NSPredicate结合。IOS斯威夫特
my coredata和/或与NSPredicate结合。IOS斯威夫特,ios,objective-c,swift,core-data,Ios,Objective C,Swift,Core Data,my coredataget查询的动态或谓词 var predicate = [NSPredicate]() //this three is or predicate.append(NSPredicate(format: "item_parent_id = %i", 1)) predicate.append(NSPredicate(format: "item_parent_id = %i", 3)) predicate
get
查询的动态或
谓词
var predicate = [NSPredicate]()
//this three is or
predicate.append(NSPredicate(format: "item_parent_id = %i", 1))
predicate.append(NSPredicate(format: "item_parent_id = %i", 3))
predicate.append(NSPredicate(format: "item_parent_id = %i", 5))
// but this one is and
predicate.append(NSPredicate(format: "price_date CONTAINS[cd] %@", 'timestamp'))
var compound = NSCompoundPredicate.orPredicateWithSubpredicates(predicate)
前三个约束用于OR运算符,最后一个约束用于日期和
我花了相当多的时间在这方面,我是swift开发的新手,我不习惯于Objective C,这就是为什么我很难将我在Objective C中发现的东西翻译成swift
如果可能,用swift写下答案,任何评论和建议都会大有帮助。提前谢谢
如果要查找类似
(a或b或c)和d
的操作,则需要两个复合谓词:
var orList = [NSPredicate]()
//this three is or
orList.append(NSPredicate(format: "item_parent_id = %i", 1))
orList.append(NSPredicate(format: "item_parent_id = %i", 3))
orList.append(NSPredicate(format: "item_parent_id = %i", 5))
let orPredicate = NSCompoundPredicate.orPredicateWithSubpredicates(orList)
// but this one is and
let other = NSPredicate(format: "price_date CONTAINS[cd] %@", 'timestamp')
let compound = NSCompoundPredicate.andPredicateWithSubpredicates([orPredicate, other])
“或”部分可以简化为一个谓词
NSPredicate(格式:“item\u parent\u id IN%@,[1,3,5])
…哇,我从没想过这这么简单,我会试试的..谢谢先生