我有一个收藏夹按钮,单击该按钮会将特定角色的图像添加到 CoreData。
@IBAction func favButtonClicked(_ sender: UIButton) {
if sender.isSelected != true {
saveFav()
}
}
func saveFav() {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let newFav = NSEntityDescription.insertNewObject(forEntityName: "Favorite", into: context)
newFav.setValue((figure?.image)!, forKey: "favFig")
do {
try context.save()
print("Saved!")
} catch {
//Process Error
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是如何在再次单击按钮时从 CoreData 中删除该图像?
我的JSON数据文件已经按字母顺序排列,并正确显示在TableView中。我使用以下方法设置板块标题:
struct FigureStats: Decodable {
let name: String
let number: String
let year: String?
}
var figSectionTitles = [String]()
var figures = [FigureStats]()
override func viewDidLoad() {
super.viewDidLoad()
figSectionTitles = [String](arrayLiteral: "#", "A", "B", "C")
}
func tableView( tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return figSectionTitles [section]
}
func tableView( tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return figures.count
}
func downloadJSON(completed: @escaping () -> ()) {
let url = URL(string: "https://xxxx")
URLSession.shared.dataTask(with: url!) { (data, response, …Run Code Online (Sandbox Code Playgroud)