Ios 检查核心数据数据库中保存的ID,并在Swift 3中显示/隐藏按钮“添加到Fav”

Ios 检查核心数据数据库中保存的ID,并在Swift 3中显示/隐藏按钮“添加到Fav”,ios,swift,core-data,Ios,Swift,Core Data,我有一个纵断面图控制器VC1,在这个VC1上我有一个按钮,上面写着“添加到收藏夹”。当我点击此按钮时,我能够将此详细信息ID和配置文件名称保存在核心数据数据库中,并且能够在我的Fav view Controller VC2内的表列表视图中查看此数据。现在,当我转到VC2时,我可以看到表列表中的所有收藏夹,当我单击其中一条记录时,它将显示该配置文件的详细信息 但现在的问题是,我在这里添加到收藏夹的按钮,我需要将其更改为从收藏夹中删除,因为此配置文件已标记为收藏夹 我能够理解obj ID需要在数据库

我有一个纵断面图控制器VC1,在这个VC1上我有一个按钮,上面写着“添加到收藏夹”。当我点击此按钮时,我能够将此详细信息ID和配置文件名称保存在核心数据数据库中,并且能够在我的Fav view Controller VC2内的表列表视图中查看此数据。现在,当我转到VC2时,我可以看到表列表中的所有收藏夹,当我单击其中一条记录时,它将显示该配置文件的详细信息

但现在的问题是,我在这里添加到收藏夹的按钮,我需要将其更改为从收藏夹中删除,因为此配置文件已标记为收藏夹

我能够理解obj ID需要在数据库中检查的概念,如果存在匹配项,则Button.setTitle将更改并执行所需的功能。但我无法正确地理解这一点

谢谢你的时间,真的很感激

我保存数据的代码

 @IBOutlet weak var fav_remove_fav_button_label: UIButton!


@IBAction func saveFav(_ sender: Any) {

    var proID = saved_id
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    let task = FavProfile(context: context) // Link Task & Context
    task.busName = bussinessName
    task.profileID = Int32(id!)!
    print ("saved id is: - \(task.profileID)")
    print ("saved profile name is: - \(task.busName)")
    fav_remove_fav_button_label.setTitle("Remove From Favourite", for: .normal)

    // Save the data to coredata
    (UIApplication.shared.delegate as! AppDelegate).saveContext()

    // let _ = navigationController?.popViewController(animated: true)

    let alert = UIAlertController(title: "Alert", message: "Added to your Favourite list", preferredStyle: UIAlertControllerStyle.alert)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
    self.present(alert, animated: true, completion: nil)


}
获取和检查数据的函数

func isExist(id: Int) -> Bool {
    var error: NSError?
    var moc: NSManagedObjectContext!
    var fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "FavProfile")
    if let results = try! moc.fetch(fetchRequest) as? [FavProfile] {

        if !results.isEmpty {
            for x in results {
                if x.profileID  == Int32(id) {
                    print("already exist")
                    moc.delete(x)
                }
            }
        }
    } else {
        print(error)
    }

    return false
}
试试这个->

  func isExist(id: Int) -> Bool {

     let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

    var error: Error? = nil
    let fetch = NSFetchRequest<NSFetchRequestResult>()
    let entityDescription = NSEntityDescription.entity(forEntityName: "FavProfile", in: context)
    fetch.entity = entityDescription
        as? NSEntityDescription!
        NSEntityDescription()
    fetch.predicate = NSPredicate(format: "profileID == %d", id)
    let fetchedObjects: [Any]? = try? context.fetch(fetch)
    if error != nil {
        return false
    }
    else {
        if (fetchedObjects?.count)! > 0 {
              print("fetchedObjects--->found");

            return true
        }
        else {
             print("fetchedObjects--->nil");
            return false
        }
    }
}


override func viewDidLoad() {
    super.viewDidLoad()
  //  favButtonLabel()

 //   isExist(id: 182397)

    let FavCheck: Bool = isExist(id:182397)
    if FavCheck == true {
        print("favourite--->");
    }
}

您是否根据profileID获取FavProfile?在bool方法中,返回true。如果发现相同的profileIDI,我尝试了true/false,但在输出面板中得到的错误是:致命错误:在展开可选的value@VarinderSingh您好,我仍然无法正确理解这一点。@VarinderSingh我试图研究您的示例解决方案,但无效。请让我们在聊天中继续讨论。