List item
So I'm trying to set up a request with multiple conditions in Swift. The SQL equivalent to:
select BOARDID
from BOARD
where BOARDID not like "someBoard"
and BOARDID not like "anotherBoard"
..
I have an array of strings and I'm trying to iterate over each to create a subPredicate, add it to a compoundPredicate, then create a fetch request using that compoundPredicate:
let openBoards = ["someBoard", "anotherBoard", "etc"],
request = NSFetchRequest(entityName: "Board")
var openBoardsSubPredicates: Array = [],
error: NSError? = nil
for board in openBoards {
var subPredicate = NSPredicate(format: "boardID not like '\(board)'")
openBoardsSubPredicates += subPredicate
}
request.predicate = NSCompoundPredicate.andPredicateWithSubpredicates(openBoardsSubPredicates)
However, it fails at the var subPredicate line..
Thanks!
The error tells you what the problem is. The format you are providing is not a valid format. Specifically, there is no "not like" comparison. You have to do the "not" outside:
NSPredicate(format: "not (boardID like %@)", board)
As @MartinR mentioned below, you can have NSPredicate automatically escape special characters so they don't interfere with the predicate by having it insert the variable instead of using string interpolation.