//
// 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)")
}
} }

最新文章

  1. [LeetCode] Ternary Expression Parser 三元表达式解析器
  2. 浅谈CSS hack(浏览器兼容)
  3. PAT1014——福尔摩斯的约会
  4. [大牛翻译系列]Hadoop(17)MapReduce 文件处理:小文件
  5. Go在linux下的安装
  6. window的cmd窗口运行git
  7. oracle的sql优化
  8. Keil 3光标问题 以及汉字问题
  9. delphi 基础书籍推荐
  10. Microsoft SQL server2017初次安装与使用记录
  11. Google浏览器设置搜索打开新的标签页
  12. Node.js使用supervisor遭遇‘supervisor’不是内部或外部命令,如果解决?
  13. easyui下载包详解
  14. input 特殊字符限制
  15. Lodop打印旋转180度 倒着打
  16. Linux 网络侦错:无法联机原因分析
  17. ShrePoint 迁移域控
  18. linux 时间戳
  19. VTK三维点集轮廓凸包提取
  20. 关于Integer的比较,今天又犯了一个低级错误,记录下

热门文章

  1. 移动web开发问题和优化小结
  2. 【linux高级程序设计】(第十一章)System V进程间通信 3
  3. TCP/IP、Http、Socket 简单理解
  4. MY97 日期控件只输入今天之前的值
  5. [BZOJ1260][CQOI2007]涂色paint 区间dp
  6. Netty源码学习(七)FastThreadLocal
  7. HDU 2795.Billboard-完全版线段树(区间求最值的位置、区间染色、贴海报)
  8. Mysql 通用二进制包安装
  9. 模板—字符串—后缀自动机(后缀自动机+线段树合并求right集合)
  10. HDU-6315 Naive Operations//2018 Multi-University Training Contest 2___1007 (线段树,区间除法)