swift - 如何检索满足条件 swift 2 的 CoreData

标签 swift core-data swift2 nsfetchrequest executefetchrequest

我有一个包含如下字段的数据库

纬度经度标志 54.1425 98.2564 0 52.1036 94.1458 3 51.1569 92.1458 3 50.1326 91.1458 3 56.1236 93.1458 0

我需要检索标记为 3 的记录。

    let fetchRequest = NSFetchRequest()

    let appDelegate =
    UIApplication.sharedApplication().delegate as! AppDelegate
    let managedContext = appDelegate.managedObjectContext

    let entityDescription = NSEntityDescription.entityForName("TABLE_LOCATION", inManagedObjectContext: managedContext)




    // Configure Fetch Request
    fetchRequest.entity = entityDescription


    do {
        let result = try managedContext.executeFetchRequest(fetchRequest)
        //print(result)


    } catch {
        let fetchError = error as NSError
        print(fetchError)
    }

我所拥有的只是上面的代码,它从表 TABLE_LOCATION 中检索所有记录,非常感谢任何代码或工作示例

谢谢!

最佳答案

您需要将 NSPredicate 添加到您的 fetchRequest。

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSPredicate_Class/

fetchRequest.predicate = NSPredicate(format: "flag == %@", "3")

关于swift - 如何检索满足条件 swift 2 的 CoreData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36262618/

相关文章:

ios - 使用 NSPredicate 在 NSDate 之间进行过滤

ios - CoreData IOS9 错误?

swift - 当 AsyncImage SwiftUI 中的图像 URL 发生变化时,如何监听新阶段的成功

swift - UITableView 需要删除可能重新排序的行

ios - 核心数据 : Trouble getting the managedObjectContext and model

swift - 从 Today Extension 保存到 Core Data 时,只能从 Widget 访问数据 - 而不是从主应用程序

ios - 将项目目标升级到 iOS 9.0

ios - 如何使用 swift 2.2 让警报 View 出现在 View Controller 的中心

ios - 使用 iOS Swift 隐藏三个按钮中的一个按钮

swift - 比较两个列表以查看缺少的内容