gpt4 book ai didi

ios - 快速核心数据

转载 作者:行者123 更新时间:2023-11-28 08:27:21 26 4
gpt4 key购买 nike

你好,我在 Swift 中遇到核心数据问题,这没有显示错误,但当我认为它应该在控制台中返回 'Joe' + 'pass' 时也不会打印任何内容。有谁能帮忙吗?

import UIKit
import CoreData

class ViewController: UIViewController {



override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

var appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

var context:NSManagedObjectContext = appDel.managedObjectContext

var newUser = NSEntityDescription.insertNewObjectForEntityForName("Users", inManagedObjectContext: context) as NSManagedObject

newUser.setValue("Joe", forKey: "username")

newUser.setValue("pass", forKey: "password")

do {
try context.save()
} catch let error {
print("Couldn't save user data")
print(error)
}

let request = NSFetchRequest(entityName: "Users")
request.returnsObjectsAsFaults = false

do {
var results = try context.executeFetchRequest(request)
results = results as! [NSManagedObject]
if results.count > 0 {
for results in results {
print(results)
}}
} catch let error as NSError {
print(error)
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

最佳答案

  1. 您应该创建自己的核心数据类(作为 NSManagedObject 的子类),然后从该类创建数组。

  2. 准确指定您要打印的内容。在你的情况下

    newUser.username

关于ios - 快速核心数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39639065/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com