2016-02-03 7 views
4

爲古怪的標題道歉。我有一個Ints數組,我想定義一個NSPredicate,它將過濾掉connectionType等於數組中包含的值的項目。來自數組元素的NSPredicate IN查詢

所以基本上是這樣的:

fetchRequest.predicate = NSPredicate(format: "connectionType IN { all items in array } 

,我發現了一些Objective C的例子,我認爲這個交易,但我剛剛進入iOS開發,並只與斯威夫特的工作,讓我們有所幫助會不勝感激:)

我試着這樣做:

fetchRequest.predicate = NSPredicate(format: "connectionType IN \(myIntArray)" 

然而,這將返回NSInvalidArgumentException。我覺得我很接近。

NSInvalidArgumentException', reason: 'Unable to parse the format string "connectionType IN [28, 28]" 

如果我做到以下幾點:

fetchRequest.predicate = NSPredicate(format: "connectionType IN %@", myArray) 

我得到這個錯誤,而不是:

NSInvalidArgumentException', reason: 'unimplemented SQL generation for predicate : (connectionType IN {28, 28})' 

回答

9

你會建造這樣的斷言:

NSPredicate(format:"connectionType IN %@", yourIntArray) 

希望這很有幫助。

+0

感謝您的建議,它似乎幾乎工作。然而,我得到以下錯誤:NSInvalidArgumentException',原因:'無法解析格式字符串'connectionType IN [28,28]「 – ardevd

+0

沒關係,它一旦我意識到我有一對多的關係,並相應地進行調整。謝謝! – ardevd