首先,我对XCode很陌生。我是一名参加编码课的高中生。我无法让我的代码使用过时的Swift核心数据教程
这里是教程:https://www.raywenderlich.com/115695/getting-started-with-core-data-tutorial
能否请你帮我更新我的代码,以适应新雨燕更新?
错误: “类型的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.
}
}
请添加您到达张贴 – muescha
OK了错误,我已经更新了! –
请同时包含您的AppDelegate.swift文件的内容,并描述弹出的“更多错误” – jrturton