gpt4 book ai didi

swift - 使用核心数据的收藏夹按钮

转载 作者:行者123 更新时间:2023-11-30 10:44:19 24 4
gpt4 key购买 nike

我只需要向数据库添加一个条目。如果数据库为空,则由于某种原因不添加任何内容。如果数据库中有记录,那么由于周期原因,我添加了几个相同的记录。这是我的代码:

import UIKit
import AVFoundation
import CoreData

class PlayerViewController: UIViewController, VLCMediaPlayerDelegate {
@IBOutlet var favoriteBarButton: UIBarButtonItem!

@IBAction func addFavorites(_ sender: UIBarButtonItem) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "MarkersModel", in: context)

let request: NSFetchRequest<MarkersModel> = MarkersModel.fetchRequest()
request.returnsObjectsAsFaults = false
request.includesPropertyValues = false
do { let result = try context.fetch(request)

for (i, s) in result.enumerated() {
// Can simply use 'sender' here, no need to index into senderArray.
if s.nameCam == self.webcamTitle! {
let value = result[i]
context.delete(value)
self.favoriteBarButton?.tintColor = .red
//do {
try context.save()
//}
}
if s.nameCam != self.webcamTitle!
{
let newFavoriteCamera = NSManagedObject(entity: entity!, insertInto: context)
newFavoriteCamera.setValue(self.webcamTitle, forKey: "nameCam")
newFavoriteCamera.setValue(self.webcamURL, forKey: "urlCam")
self.favoriteBarButton?.tintColor = .green
//do {

//let value = result[i]
context.insert(newFavoriteCamera)
try context.save()
//}
}
}
}


enter image description here

最佳答案

我做了我想做的。这是我对任何有兴趣的代码。它有效=)

@IBAction func addFavorites(_ sender: UIBarButtonItem) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "MarkersModel", in: context)
let request: NSFetchRequest<MarkersModel> = MarkersModel.fetchRequest()
request.returnsObjectsAsFaults = false
var array: [String] = []

do {
let result = try context.fetch(request)
for data in result {
array.append(data.nameCam!)
print(array)
}
}catch {
print(error)}
do {
let newFavoriteCamera = NSManagedObject(entity: entity!, insertInto: context)
newFavoriteCamera.setValue(self.webcamTitle, forKey: "nameCam")
newFavoriteCamera.setValue(self.webcamURL, forKey: "urlCam")
let result = try context.fetch(request)

if array.contains(self.webcamTitle) {
for data in result {
if data.nameCam == self.webcamTitle {
favoriteBarButton?.tintColor = .red
print("YES")
context.delete(data)
try context.save()
}
}
} else{
print("NO")
self.favoriteBarButton?.tintColor = .green
try context.save()
print(array)
}
} catch {
print(error)}
}


func RequestFromDB() {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "MarkersModel", in: context)

let request: NSFetchRequest<MarkersModel> = MarkersModel.fetchRequest()
request.returnsObjectsAsFaults = false

var array1: [String] = []

do {
let result = try context.fetch(request)
for data in result {
array1.append(data.nameCam!)
}
} catch {
print(error)}

print(array1)
//let u = "Maine, Caribouw"
if array1.contains(self.webcamTitle) {
print("YES")
self.favoriteBarButton?.tintColor = .green
} else{
print("NO")
self.favoriteBarButton?.tintColor = .red
}


}
override func viewDidLoad() {
super.viewDidLoad()
RequestFromDB()


}

关于swift - 使用核心数据的收藏夹按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56082530/

24 4 0
文章推荐: javascript - 如何通过类名和类的子类使用 JS innerHTML
文章推荐: javascript - 有选择地从
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com