我下面的快速代码需要 2 个图像文字,然后保存到核心数据中。我只想在 func fetch 中打印 dubgg 区域中有多少属性保存到核心数据中。所以在调试部分它应该说在调试部分保存了 2 个项目。
import UIKit
import CoreData
class ViewController: UIViewController {
let appDelegate = UIApplication.shared.delegate as! AppDelegate //Singlton instance
lazy var context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
var piczy = [UIImage]()
override func viewDidLoad() {
super.viewDidLoad()
let newUser = User(context: context)
let newUser2 = User(context: context)
newUser.pic = #imageLiteral(resourceName: "Jessica").jpegData(compressionQuality: 0.9) as NSObject?
newUser2.pic = #imageLiteral(resourceName: "Jessica").jpegData(compressionQuality: 0.9) as NSObject?
save()
}
func save() {
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "User")
do {
let jake = try context.count(for: fetchRequest)
print(jake)
} catch let error as NSError {
print("Could not fetch \(error) ")
}
}