Search code examples
iosswifticloud

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


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-


Solution

  • 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