- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要一些帮助来快速创建收藏夹按钮。我一直在努力研究如何使用核心数据,我认为我已经很好地掌握了它,期望我很难用它来创建收藏夹按钮。我有一个示例项目,它有两个 View ,一个有几个数字的 TableView 和一个有按钮的 View Controller 。
看完视频后,我被告知要获取从 appDelegate 保存数据的核心数据函数,并将其放在一个新 View 中,我称之为 PersistenceService.swift
import Foundation
import CoreData
class PersistenceService {
private init() {}
static var context: NSManagedObjectContext {
return persistentContainer.viewContext
}
static var persistentContainer: NSPersistentContainer = {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
let container = NSPersistentContainer(name: "otherButton")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
// MARK: - Core Data Saving support
static func saveContext () {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
print("SAVED")
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
}
第一个 View ,tableview,很简单,我只是创建了一个包含三个单元格的表格,当按下这些单元格时会转到一个新 View 。
idTableView.swift
import UIKit
import CoreData
class idTableView: UITableViewController {
var idNumber = [1, 2, 3]
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "segue", sender: indexPath)
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return idNumber.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! idCell
cell.idCell.text = "\(idNumber[indexPath.row])"
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segue" {
let nxtVC = segue.destination as? ViewController
let myIndexPath = self.tableView.indexPathForSelectedRow!
let row = myIndexPath.row
nxtVC?.idNumber = idNumber[row]
}
}
当按下 idTableView.swift 中的单元格时,它会将您带到一个新 View ,中间有一个空的星形按钮。
ViewController.swift
import UIKit
import CoreData
class ViewController: UIViewController {
var buttonIsSelected = false
@IBOutlet var navigation: UINavigationItem!
var idNumber: Int?
@IBOutlet var onOffButton: UIButton!
let image1 = UIImage(named: "empty") as UIImage?
let image2 = UIImage(named: "filled") as UIImage?
override func viewDidLoad() {
super.viewDidLoad()
getId()
}
//button Action
@IBAction func onOffButtonPressed(_ sender: Any) {
buttonIsSelected = !buttonIsSelected
if buttonIsSelected == true {
getFavorite(bool: buttonIsSelected)
print("favorited")
onOffButton.setImage(image2, for: .normal)
} else if buttonIsSelected == false {
getFavorite(bool: buttonIsSelected)
print("unfavorited")
onOffButton.setImage(image1, for: .normal)
}
}
func getId() {
if let id = idNumber {
let newNumber = NSEntityDescription.insertNewObject(forEntityName: "IdNumber", into: PersistenceService.context) as! IdNumber
let newBool = NSEntityDescription.insertNewObject(forEntityName: "Favorite", into: PersistenceService.context) as! Favorite
newNumber.setValue(Int16(id), forKey: "idNumber")
newBool.setValue(buttonIsSelected, forKey: "favorite")
newNumber.favorite = newBool
print("newNumber: \(newNumber.favorite)")
print("newBool: \(newBool)")
PersistenceService.saveContext()
//self.navigationItem.title = "\(id)"
}
}
func getFavorite(bool: Bool) {
let newFavorite = NSEntityDescription.insertNewObject(forEntityName: "Favorite", into: PersistenceService.context)
newFavorite.setValue(bool, forKey: "favorite")
PersistenceService.saveContext()
}
我看到的核心数据就像创建一个工作场所应用程序,其中有父级部门,然后是填充部门的人员。这些应用程序创建一个部门,然后将人员添加到该部门,然后填充一个 tableView。我想做的是不同的,因为在这种情况下我的 parent 是身份证号码,每个身份证号码只有一个 bool 值。更改 bool 时,它会将按钮的图像更改为实心星形。我遇到的麻烦是将每个 bool 值保存到每个 ID 号。例如,我有三个 ID 号,这三个 ID 号中的每一个都可以有一个 bool 值,例如 1:true,2:false。 3:真实。每个 id 号的默认值都是 false,但是当按下按钮时,它应该保存 bool 的变化,这反过来又改变了按钮的图像。
我认为我遇到的问题是在代码中建立正确的关系。似乎我可以为单个 View 执行此操作,但是当我转到另一个 View 时,应用程序崩溃或其他原因,但我在更改为默认状态之前保存的按钮状态是错误/空星按钮。
如果有人有问题,我会尽我所能回答。感谢您的帮助。
最佳答案
我会以不同的方式设计它:出于您的目的,您只需要一个 CD 对象,它具有属性 idNumber
和 Bool isFavorite
。当您加载您的 tableView
时,您将从 CD 中获取所有相关对象并将它们保存在您的 idNumber
数组中。然后,您可以轻松访问所选 TableView 单元格的数组元素,只需设置 isFavorite
,然后保存您的 CD 上下文。
如果你想获取所有带有 isFavorite == true
的对象,你可以简单地使用具有谓词的 Request 执行提取...
当 getID
被调用时,您每次创建新对象的方式是什么?一旦创建,你当然想重用它......否则你将拥有多个具有相同 idNumber 的对象......
顺便说一句:完全没有必要自己为CD对象创建ID。默认情况下,所有 CD 对象都有一个唯一的 ID。在 CD 中,您只需关心您的自然属性和关系。
关于ios - 在 swift 中创建收藏按钮的核心数据关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49524865/
Linux 有许多跨(假设是 2 个)CPU 内核执行的线程和进程。我希望我的单线程 C/C++ 应用程序成为 CPU0 上的唯一线程。我如何“移动”所有其他线程以使用 CPU1? 我知道我可以使用
我有一个类似于下图的数据库表 Table with 2 columns (UserId and value) 我将传递 UserId 和 2 个字符串。例如:userId: 1, key1: h1,
我想在我的新项目中使用 ASP.NET Core,因为我听说它更快。但是,该项目将使用广泛的数据库访问功能,Entity Framework Core 不支持其中一些功能。我想知道,是否可以使用 En
我已经使用 EntityFrameworkCore.SqlServer 2.0 开发了 asp .net core wep api 2.0 应用程序。它是使用数据库优先方法开发的。当尝试使用 dbco
我已经阅读了很多关于这个主题的文章,但我仍然无法处理这个问题。对不起,如果它是重复的,无论如何! 所以基本上,我正在从头开始构建一个 Angular 应用程序,并且我想按照最佳约定来组织我的代码。我有
我对MPI还是陌生的,所以如果这是一个琐碎的问题,请原谅我。我有一个四核CPU。我想运行一个在单个内核上使用两个进程的OpenMPI C++程序。有什么办法吗?如果是这样,那又如何?我提到了this
下面是一个传播异常处理机制的类问题,所需的输出是异常。任何人都可以解释为什么输出是异常,在此先感谢。 Class Question { public void m1() throws Excep
我想打印每个获得 CPU 时间片的进程的 name 和 pid。可能吗? 最佳答案 对于单个流程,您可以在以下位置获取此信息: /proc//stat 第14和第15个字段分别代表在用户态和内核态花费
我想知道是否可以识别具有特定 thread-id 的线程使用的物理处理器(核心)? 例如,我有一个多线程应用程序,它有两 (2) 个线程(例如,thread-id = 10 和 thread-id =
我有一个需要身份验证的 Solr 核心。假设我有一个用户,密码为password。当我现在尝试在控制台中创建一个 Solr 核心时 bin\solr create -c test 我收到 HTTP 错
我想为与使用它的项目不同的类库中的第二个和后续数据库创建迁移。有皱纹。我永远不会知道连接字符串,直到用户登录并且我可以从目录数据库 (saas) 中获取它。 对于目录数据库,我使用了来自 this 的
我想为一种可以产生 GHC Core 的简单语言创建一个前端。然后我想获取这个输出并通过正常的 GHC 管道运行它。根据this page , 不能直接通过 ghc 命令实现。我想知道是否有任何方法可
阅读文档,我构建了 2 个使用 BLE 连接 2 个 iDevices 的应用程序。 一个设备是中央设备,另一个是外围设备。 Central在寻找Peripheral,当找到它时,探索它的服务和特性,
在我的网络应用程序中,我对长时间运行的任务进行了操作,我想在后台调用此任务。因此,根据文档 .net core 3.1 Queued background tasks我为此使用这样的代码: publi
Solr 1.4 Enterprise Search Server 建议对核心副本进行大量更新,然后将其换成主核心。我正在按照以下步骤操作: 创建准备核心:http://localhost:8983/
它们是否存在,如果存在,文档和代码在哪里? 最佳答案 它们位于 Git 的 test 目录中。 https://github.com/jquery/jquery/tree/master/test 关于
我有一个 Lisp (SBCL 1.0.40.0.debian) 应用程序 (myfitnessdata),它使用以下代码来处理命令行参数: (:use :common-lisp) (:export
Core是GHC的中间语言。阅读Core可以帮助你更好地了解程序的性能。有人向我索要有关阅读 Core 的文档或教程,但我找不到太多。 有哪些文档可用于阅读 GHC Core? 这是我迄今为止发现的内
我有一个核心 WebJob 部署到 Azure Web 应用程序中。我正在使用WebJobs version 3.0.6 . 我注意到,WebJob 代码不会立即拾取对连接字符串和应用程序设置的更改(
我有一个在内部构造和使用 SqlConnection 类的第三方库。我可以从该类继承,但它有大量重载,到目前为止我一直无法找到合适的重载。我想要的是将参数附加到正在使用的连接字符串。 有没有办法在 .
我是一名优秀的程序员,十分优秀!