首先,我对XCode很新。我是一名参加编码课程的高中学生。
以下是教程:https://www.raywenderlich.com/115695/getting-started-with-core-data-tutorial
是否可以帮我更新我的代码以适应新的Swift更新?
错误:“AppDelegate类型的值没有成员managedObjectContext”
排队给我错误:
let appDelegate =
UIApplication.shared.delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
然而,当我删除它,并用
替换它let delegate = UIApplication.shared.delegate as! AppDelegate
let managedObjectContext = delegate.persistentContainer.viewContext
弹出更多错误。
我的完整代码:
import UIKit
import CoreData
class ViewController: UIViewController, UITableViewDataSource
{
@IBOutlet weak var tableView: UITableView!
@IBAction func addName(_ sender: Any)
{
let alert = UIAlertController(title: "New Name", message: "Add a new name", preferredStyle: .alert)
let saveAction = UIAlertAction(title: "Save", style: .default, handler:
{
(action:UIAlertAction) -> Void in
let textField = alert.textFields!.first
self.saveName(name: textField!.text!)
self.tableView.reloadData()
})
let cancelAction = UIAlertAction(title: "Cancel", style: .default) {
(action: UIAlertAction) -> Void in
}
alert.addTextField
{
(textField: UITextField) -> Void in
}
alert.addAction(saveAction)
alert.addAction(cancelAction)
present(alert, animated: true, completion: nil)
}
func saveName(name: String)
{
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let entity = NSEntityDescription.entityForName("Person", inManagedObjectContext:managedContext)
let person = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedContext)
person.setValue(name, forKey: "name")
do
{
try managedContext.save()
people.append(person)
}
catch let error as NSError
{
print("Could not save \(error), \(error.userInfo)")
}
}
var people = [NSManagedObject]()
override func viewDidLoad()
{
super.viewDidLoad()
title = "\"The List\""
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
// Do any additional setup after loading the view, typically from a nib.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//1
let appDelegate =
UIApplication.shared.delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
//2
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Person")
//3
do {
let results =
try managedContext.executeFetchRequest(fetchRequest)
people = results as! [NSManagedObject]
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
}
func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return people.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
let person = people[indexPath.row]
cell!.textLabel!.text = person.value(forKey: "name") as? String
return cell!
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:0)
您可以使用一个简单的库(由我编写)来获取最新的Swift语法AACoreData。 只需在安装库后分配数据模型,
AACoreData.sharedInstance().dataModel = "YourEntity"
有关更多指南和文档,请访问官方存储库AACoreData。
答案 1 :(得分:0)