first core data
2024-08-25 23:11:23
//
// TableViewController.swift
// TestCoreData
//
//
import CoreData import UIKit class TableViewController: UITableViewController {
//懒加载 需指定 该变量的类型是 AppDelegate
lazy var appDelegate:AppDelegate = {
let app = UIApplication.sharedApplication().delegate as! AppDelegate
return app
}()
// var peoples = [NSManagedObject]()
var peoples = [Person]() // MARK: - add a name
@IBAction func add(sender: UIBarButtonItem) { let alert = UIAlertController(title: "add a name", message: "", preferredStyle: .Alert)
let addAction = UIAlertAction(title: "add", style: UIAlertActionStyle.Default) { (action :UIAlertAction) -> Void in let name = alert.textFields![].text
let age = alert.textFields![].text! as NSString
self.saveName(name!,age: age.integerValue)
let indexPath = NSIndexPath(forRow: self.peoples.count-, inSection: )
self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Left) } let cancelAction = UIAlertAction(title: "cancel", style: .Default, handler: nil) alert.addAction(addAction)
alert.addAction(cancelAction) alert.addTextFieldWithConfigurationHandler(nil)
alert.addTextFieldWithConfigurationHandler(nil) self.presentViewController(alert, animated: true, completion: nil) }
func saveName(name:String,age:Int){
let context = appDelegate.managedObjectContext let person = NSEntityDescription.insertNewObjectForEntityForName("Person", inManagedObjectContext: context) as! Person;
person.name = name
person.age = age // let entity = NSEntityDescription.entityForName("Person", inManagedObjectContext: context)
// let person = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: context) // person(name, forKey: "name") do{
try context.save()
peoples.append(person)
}catch let error as NSError{
print("could not save object\(error) , \(error.userInfo)")
} } override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
} // MARK: - Table view data source override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return
} override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return peoples.count
} override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("mycell", forIndexPath: indexPath)
// cell.textLabel?.text = peoples[indexPath.row].valueForKey("name") as? String
let name = peoples[indexPath.row].name
let age = peoples[indexPath.row].age
cell.textLabel?.text = "\(name!) -----> \(age!) 岁"
return cell
} override func viewDidLoad() {
self.title = "name list"
let context = appDelegate.managedObjectContext
let request = NSFetchRequest(entityName: "Person")
do{
// let result = try context?.executeFetchRequest(request) as! [NSManagedObject]
// self.peoples = result
let result = try context.executeFetchRequest(request) as! [Person]
self.peoples = result
} catch let error as NSError{
print("could not fecth \(error) , \(error.userInfo)")
}
} }
最新文章
- [LeetCode] Ternary Expression Parser 三元表达式解析器
- 浅谈CSS hack(浏览器兼容)
- PAT1014——福尔摩斯的约会
- [大牛翻译系列]Hadoop(17)MapReduce 文件处理:小文件
- Go在linux下的安装
- window的cmd窗口运行git
- oracle的sql优化
- Keil 3光标问题 以及汉字问题
- delphi 基础书籍推荐
- Microsoft SQL server2017初次安装与使用记录
- Google浏览器设置搜索打开新的标签页
- Node.js使用supervisor遭遇‘supervisor’不是内部或外部命令,如果解决?
- easyui下载包详解
- input 特殊字符限制
- Lodop打印旋转180度 倒着打
- Linux 网络侦错:无法联机原因分析
- ShrePoint 迁移域控
- linux 时间戳
- VTK三维点集轮廓凸包提取
- 关于Integer的比较,今天又犯了一个低级错误,记录下
热门文章
- 移动web开发问题和优化小结
- 【linux高级程序设计】(第十一章)System V进程间通信 3
- TCP/IP、Http、Socket 简单理解
- MY97 日期控件只输入今天之前的值
- [BZOJ1260][CQOI2007]涂色paint 区间dp
- Netty源码学习(七)FastThreadLocal
- HDU 2795.Billboard-完全版线段树(区间求最值的位置、区间染色、贴海报)
- Mysql 通用二进制包安装
- 模板—字符串—后缀自动机(后缀自动机+线段树合并求right集合)
- HDU-6315 Naive Operations//2018 Multi-University Training Contest 2___1007 (线段树,区间除法)