Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 核心数据插入_Swift_Xcode_Core Data - Fatal编程技术网

Swift 核心数据插入

Swift 核心数据插入,swift,xcode,core-data,Swift,Xcode,Core Data,我试着学习核心数据,经过许多小时和大量教程,我仍然感到困惑。所有的教程似乎都很复杂,涉及到很多元素。如果有人能给我一个最简单的插入示例,那将非常有帮助。(请不要使用表格、导航控制器…只需插入名称和密码) 我已经用属性名和密码创建了一个用户实体 我有两个文本字段:名称和密码。我有从故事板到ViewController的连接 我有一个钮扣 当用户单击“到”按钮时,如何保存名称和密码 @IBOutlet weak var name: UITextField! @IBOutlet weak var pa

我试着学习核心数据,经过许多小时和大量教程,我仍然感到困惑。所有的教程似乎都很复杂,涉及到很多元素。如果有人能给我一个最简单的插入示例,那将非常有帮助。(请不要使用表格、导航控制器…只需插入名称和密码)

我已经用属性名和密码创建了一个用户实体 我有两个文本字段:名称和密码。我有从故事板到ViewController的连接 我有一个钮扣

当用户单击“到”按钮时,如何保存名称和密码

@IBOutlet weak var name: UITextField!
@IBOutlet weak var password: UITextField!

@IBAction func button(_ sender: UIButton) {

}

首先,此时是否有可用的
NSManagedObjectContext
?假设您这样做了,它被称为
context

import UIKit
import CoreData

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        if let viewController = window?.rootViewController as? ViewController {
            viewController.context = persistentContainer.viewContext
        }

        return true
    }

    // ... other functions
}
非常简单的例子

在视图控制器中惰性地实例化上下文

lazy var context : NSManagedObjectContext = {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    return appDelegate.persistentContainer.viewContext
}()
如果只在
iAction
中使用它,则在其中临时实例化它(请参见注释掉的代码)

然后在IBAction中写入

@IBAction func button(_ sender: UIButton) {
    guard name.hasText, !name.text!.first!.isWhitespace,
        password.hasText, !password.text!.first!.isWhitespace else {
        // show an alert that both text fields must not be empty and must not start with a whitespace characters.
        return 
    }
    // let appDelegate = UIApplication.shared.delegate as! AppDelegate
    // let context = appDelegate.persistentContainer.viewContext
    let user = User(context: context)
    user.name = self.name.text!
    user.password = self.password.text!
    do {
        try context.save()
    } catch {
        print(error)
    }
}
旁注:为避免歧义,请将文本字段命名为更清晰的名称,例如

@IBOutlet weak var nameField: UITextField!
@IBOutlet weak var passwordField: UITextField!

我在viewDidLoad中有:让appDelegate=UIApplication.shared.delegate作为!AppDelegate let context=AppDelegate.persistentContainer.ViewContext它似乎不适合您的代码。怎么了?你确定@iAction链接到按钮了吗?是的,我已经检查了链接,它给了我一个错误:不能在属性初始值设定项中使用实例成员“appDelegate”;属性初始值设定项在“self”可用之前运行有什么地方可以检查保存的内容吗?(我来自MySql,在一个表中,我可以看到我保存的内容,并在必要时进行清理)在启动时传递的Run scheme>参数中添加
-com.apple.CoreData.SQLDebug 1
。它记录sql访问。我喜欢你检查文本字段是否有text:guard。。。但它有一个限制,即如果用户输入一个空白,它也被视为hasText(但您什么也看不到,最好返回)。你知道解决这个限制的方法吗?我添加了一个检查,检查两个文本字段不能包含任何空白字符。现在第一个字符不能是空白字符。
@IBOutlet weak var nameField: UITextField!
@IBOutlet weak var passwordField: UITextField!