Swift: 'IN' query alternative using NSPredicate for fetching records from iCloud

独自空忆成欢 提交于 2019-12-11 10:56:39

问题


In iCloud, I want to search in Item's table, where I have userId column in swift but I didn't know how

Table: User

UserId {String type}
UserName {String type}

Table: Item

ItemId {String type}
ItemName {String type}
UserId {String type}

I need NSPredicate alternative to this query

Select * from item where userId in (2,3,4)

Also, is there any way so I can fire sql query directly in iCloud?


Is this is the correct way of doing above in Swift?

var orList = [NSPredicate]()
orList.append(NSPredicate(format: "userId = %i", 2))
orList.append(NSPredicate(format: "userId = %i", 3))       
orList.append(NSPredicate(format: "userId = %i", 4))

Or is there any way to pass array of [2, 3, 4]

Thanks-


回答1:


Try this;

let usrIds:[Int] = [1,2,3];
let predicate:NSPredicate = NSPredicate(format: "userId IN %@", usrIds);

ref. https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Predicates/Articles/pUsing.html



来源:https://stackoverflow.com/questions/33775144/swift-in-query-alternative-using-nspredicate-for-fetching-records-from-icloud

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