我正在尝试检索SISLItem对象,通过谓词过滤它们,谓词用“IN”聚合参数。
import Foundation
import CoreData
@objc(SISLItem)
class SISLItem: NSManagedObject {
@NSManaged var needDate: NSDate?
@NSManaged var purchaseStatus: NSNumber
@NSManaged var isProposed: NSNumber
@NSManaged var slEntry: SISLEntry?
}
import Foundation
import CoreData
extension SISLItem {
// MARK: - Variables
enum PurchaseStatus: Int16 {
case
Cancelled = -1,
Open,
OnHold,
Purchased,
Expired
}
…
稍后,我使用它来在扩展名中创建谓词。
…
var error: NSError?
let filterStatus: [Int16] = [SISLItem.PurchaseStatus.Open.rawValue, SISLItem.PurchaseStatus.OnHold.rawValue]
let predicate: NSPredicate = NSPredicate(format: "\(SISLItem.Attribute.PurchaseStatus.name) IN %@“, argumentArray: filterStatus)
let result: [SISLItem] = CoreDataHelper.searchObjectsWithPredicate(entityName: SISLItem.entityName(), predicate: predicate, sortKey: "\(SISLItem.Attribute.ShopListEntry.name).\(SISLEntry.Attribute.Name.name)", sortAscending: true, error: &error) as! [SISLItem]
…
我收到以下错误消息:
/Users/rs186096/Documents/OneDrive/Entwicklung/XCode/Shop It/Shop It/SISLItemExt.swift:66:36: Cannot find an initializer for type ‘NSPredicate' that accepts an argument list of type '(format: String, argumentArray: [Int16])'
用 0 , 1 等整数值替换 filterStatus 中的枚举值不会导致此错误:
let filterStatus = [0, 1]
将枚举类型更改为 Int ,它可以工作:
enum PurchaseStatus: Int {
…
let filterStatus: [Int] = [SISLItem.PurchaseStatus.Open.rawValue, SISLItem.PurchaseStatus.OnHold.rawValue]
使用特定的整数类型是谓词的已知限制吗?