在 swift 3 中获取相关表

Posted

技术标签:

【中文标题】在 swift 3 中获取相关表【英文标题】:fetch related tables in swift 3 【发布时间】:2016-11-06 19:48:43 【问题描述】:

我正在尝试获取属于某个品牌的所有模型,但是当我尝试获取外键时,我得到的 NSSET 类型具有某种地址结构,其中包含外键,我是坚持这一点,这是我的代码:

class func insertNew (brandDescription: String, inManagedObjectContext context: NSManagedObjectContext) -> Car_Brand? 
        let request: NSFetchRequest<Car_Brand> = Car_Brand.fetchRequest()
        request.predicate = NSPredicate (format: "brand = %@", brandDescription)
        if let brand = (try? context.fetch(request))?.first 
            return brand
         else if let brand = NSEntityDescription.insertNewObject(forEntityName: "Car_Brand", into: context) as? Car_Brand 
            brand.brand = brandDescription
            return brand
        
        return nil
    
class func fetchModel (brandDescription: String, inManagedObjectContext context: NSManagedObjectContext) -> [String]? 
    let Brand = Car_Brand.insertNew(brandDescription: brandDescription, inManagedObjectContext: context)
    let request: NSFetchRequest<Model> = Model.fetchRequest()
    request.predicate = NSPredicate(format: "id_model = %ld", (Brand?.id_model)!)
    request.propertiesToFetch = ["model"]
    request.sortDescriptors = [NSSortDescriptor(key: "model", ascending: true)]
    if let preModelList = try? context.fetch(request) as [Model] 
        var modelList: [String] = []
        for preModelList in preModelList 
            modelList.append(preModelList.model!)
        
        return modelList
     else 
        return nil
    

您能帮我了解如何获取相关表吗?

【问题讨论】:

【参考方案1】:

您的fetchModel 每次调用它时都会插入一个Car_Brand 的新实例。新插入的实例上不太可能存在任何现有模型。

附:它们不是 Core Data 中的“表格”。我们有实体和关系。但是将 Core Data 视为数据库会让您在未来遇到很多逻辑问题。

【讨论】:

谢谢,只有在不存在时才插入,我试图将谓词修改为这样的东西,request.predicate = NSPredicate(format: "Car_Brand.id_model = %@",brandDescription) 但是也不起作用,你知道我在哪里可以找到一些好的例子来过滤这种方式【参考方案2】:

解决方案:

class func fetchModel (brandDescription: String, inManagedObjectContext context: NSManagedObjectContext) -> [String]? 
        let request: NSFetchRequest<Model> = Model.fetchRequest()
        request.predicate = NSPredicate(format: "id_model.brand = %@",brandDescription)
        request.propertiesToFetch = ["model"]
        request.sortDescriptors = [NSSortDescriptor(key: "model", ascending: true)]
        if let preModelList = try? context.fetch(request) as [Model] 
            var modelList: [String] = []
            for preModelList in preModelList 
                modelList.append(preModelList.model!)
            
            return modelList
         else 
            return nil
        
    

参考: https://developer.apple.com/library/prerelease/content/documentation/Cocoa/Conceptual/Predicates/Articles/pUsing.html

【讨论】:

以上是关于在 swift 3 中获取相关表的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Swift beta 3 中从 UIImagePickerControllerDelegate 获取 UIImage?

Swift 4 从 JSON 中获取相关数据

如何在 swift 4 中的另一个表视图控制器中获取该表视图单元格中的文本字段文本?

在CoreData ios swift3中只获取表的几个属性

swift 笔记:iOS与JavaScript的交互(二):JavaScriptCore:16。 JSContext相关演示,在委托:webViewDidFinishLoad()中获取上下文

Swift - 根据数组值从 Parse 表中加载