gpt4 book ai didi

swift - CoreData 与 Swift

转载 作者:行者123 更新时间:2023-11-30 13:52:57 25 4
gpt4 key购买 nike

我是 swift 和 core 数据的新手。我试图显示两个文本字段,一旦您单击“保存”,它们将进入核心数据并放入表格 View 中。我有一个文本字段可以工作,但我还需要另一个文本字段也可以工作。谁能告诉我我做错了什么..

我已经将这两个属性添加到核心数据中,一个是“名称”,另一个是“描述”。

这是我的 View Controller 中的内容..

谢谢!

类ViewController:UIViewController,UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!

var people = [NSManagedObject]()

@IBAction func addName(sender: AnyObject) {
let alert = UIAlertController(title: "New Client",
message: "Add a new client",
preferredStyle: .Alert)

let saveAction = UIAlertAction(title: "Save",
style: .Default,
handler: { (action:UIAlertAction) -> Void in

let textField = alert.textFields!.first
self.saveName(textField!.text!)

let textF = alert.textFields!.first
self.saveName(textF!.text!)
self.tableView.reloadData()
})

let cancelAction = UIAlertAction(title: "Cancel",
style: .Default) { (action: UIAlertAction) -> Void in
}

alert.addTextFieldWithConfigurationHandler {
(textField: UITextField) -> Void in

(textF: UITextField) -> Void in
}


alert.addAction(saveAction)
alert.addAction(cancelAction)

presentViewController(alert,
animated: true,
completion: nil)
}


override func viewDidLoad() {
super.viewDidLoad()
title = "\"Clients\""
tableView.registerClass(UITableViewCell.self,
forCellReuseIdentifier: "Cell")

}

func tableView(tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return people.count
}

func tableView(tableView: UITableView,
cellForRowAtIndexPath
indexPath: NSIndexPath) -> UITableViewCell {

let cell =
tableView.dequeueReusableCellWithIdentifier("Cell")

let person = people[indexPath.row]

cell!.textLabel!.text =
person.valueForKey("name") as? String
person.valueForKey("description") as? String

return cell!
}

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

func saveName(name: String , descriptions: String) {
//1
let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate

let managedContext = appDelegate.managedObjectContext

//2
let entity = NSEntityDescription.entityForName("Person",
inManagedObjectContext:managedContext)

let person = NSManagedObject(entity: entity!,
insertIntoManagedObjectContext: managedContext)

//3
person.setValue(name, forKey: "name")
person.setValue(descriptions, forKey: "description")

//4
do {
try managedContext.save()
//5
people.append(person)
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)

//1
let appDelegate =
UIApplication.sharedApplication().delegate as! AppDelegate

let managedContext = appDelegate.managedObjectContext

//2
let fetchRequest = NSFetchRequest(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)")
}
}

}

最佳答案

如果要在单元格的同一文本字段中显示人员的姓名和描述,则应使用 + 指定。除此之外,您通常可以直接调用该人的姓名和描述,如下所示:

person.name
person.description

试试这个:

cell!.textLabel!.text = person.name + " " + person.description

关于swift - CoreData 与 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34034057/

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