无法在swift 3中查询CoreData

时间:2016-10-13 06:27:46

标签: xcode core-data swift3

我正在使用此类函数来搜索一个字段的记录。这里的问题是它总是返回nil,如果删除谓词将返回数据库中的任何行。我正在使用Xcode v 8.0

class func visitorWithCode(code: String, inManagedObjectContext context: NSManagedObjectContext) -> Visitor? {
    print(code)

    var visitor: Visitor? = nil
    let fetchRequest: NSFetchRequest<Visitor> = NSFetchRequest(entityName: "Visitor")
    fetchRequest.predicate = NSPredicate(format: "barcodeNo == \(code)")
    fetchRequest.fetchLimit = 1
    let fetchedData = try! context.fetch(fetchRequest)
    if (!fetchedData.isEmpty) {
        print(fetchedData.first)
        visitor = fetchedData.first! as Visitor

        return visitor
    }
    else {
        return visitor
    }
}

0 个答案:

没有答案