我正在使用此类函数来搜索一个字段的记录。这里的问题是它总是返回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
}
}