1. //
  2. // TableViewController.swift
  3. // TestCoreData
  4. //
  5. //
  6. import CoreData
  7.  
  8. import UIKit
  9.  
  10. class TableViewController: UITableViewController {
  11. //懒加载 需指定 该变量的类型是 AppDelegate
  12. lazy var appDelegate:AppDelegate = {
  13. let app = UIApplication.sharedApplication().delegate as! AppDelegate
  14. return app
  15. }()
  16. // var peoples = [NSManagedObject]()
  17. var peoples = [Person]()
  18.  
  19. // MARK: - add a name
  20. @IBAction func add(sender: UIBarButtonItem) {
  21.  
  22. let alert = UIAlertController(title: "add a name", message: "", preferredStyle: .Alert)
  23. let addAction = UIAlertAction(title: "add", style: UIAlertActionStyle.Default) { (action :UIAlertAction) -> Void in
  24.  
  25. let name = alert.textFields![].text
  26. let age = alert.textFields![].text! as NSString
  27. self.saveName(name!,age: age.integerValue)
  28. let indexPath = NSIndexPath(forRow: self.peoples.count-, inSection: )
  29. self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Left)
  30.  
  31. }
  32.  
  33. let cancelAction = UIAlertAction(title: "cancel", style: .Default, handler: nil)
  34.  
  35. alert.addAction(addAction)
  36. alert.addAction(cancelAction)
  37.  
  38. alert.addTextFieldWithConfigurationHandler(nil)
  39. alert.addTextFieldWithConfigurationHandler(nil)
  40.  
  41. self.presentViewController(alert, animated: true, completion: nil)
  42.  
  43. }
  44. func saveName(name:String,age:Int){
  45. let context = appDelegate.managedObjectContext
  46.  
  47. let person = NSEntityDescription.insertNewObjectForEntityForName("Person", inManagedObjectContext: context) as! Person;
  48. person.name = name
  49. person.age = age
  50.  
  51. // let entity = NSEntityDescription.entityForName("Person", inManagedObjectContext: context)
  52. // let person = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: context)
  53.  
  54. // person(name, forKey: "name")
  55.  
  56. do{
  57. try context.save()
  58. peoples.append(person)
  59. }catch let error as NSError{
  60. print("could not save object\(error) , \(error.userInfo)")
  61. }
  62.  
  63. }
  64.  
  65. override func didReceiveMemoryWarning() {
  66. super.didReceiveMemoryWarning()
  67. }
  68.  
  69. // MARK: - Table view data source
  70.  
  71. override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  72. return
  73. }
  74.  
  75. override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  76. return peoples.count
  77. }
  78.  
  79. override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  80. let cell = tableView.dequeueReusableCellWithIdentifier("mycell", forIndexPath: indexPath)
  81. // cell.textLabel?.text = peoples[indexPath.row].valueForKey("name") as? String
  82. let name = peoples[indexPath.row].name
  83. let age = peoples[indexPath.row].age
  84. cell.textLabel?.text = "\(name!) -----> \(age!) 岁"
  85. return cell
  86. }
  87.  
  88. override func viewDidLoad() {
  89. self.title = "name list"
  90. let context = appDelegate.managedObjectContext
  91. let request = NSFetchRequest(entityName: "Person")
  92. do{
  93. // let result = try context?.executeFetchRequest(request) as! [NSManagedObject]
  94. // self.peoples = result
  95. let result = try context.executeFetchRequest(request) as! [Person]
  96. self.peoples = result
  97. } catch let error as NSError{
  98. print("could not fecth \(error) , \(error.userInfo)")
  99. }
  100. }
  101.  
  102. }

first core data的更多相关文章

  1. iOS之Core Data及其线程安全

    一.简介 Core Data是iOS5之后才出现的一个框架,它提供了对象-关系映射(ORM)的功能,即能够将OC对象转化成数据,保存在SQLite数据库文件中,也能够将保存在数据库中的数据还原成OC对 ...

  2. Core Data的一些常见用法

    一.简介 Core Data是一个纯粹的面向对象框架,其本质就是一个ORM(对象关系映射:Object Relational Mapping),能以面向对象的方式操作SQLite数据库.在实际开发中绝 ...

  3. Core Data 使用映射模型

    Core Data 使用映射模型 如果新版本的模型存在较复杂的更改,可以创建一个映射模型,通过该模型指定源模型如何映射到目标模型. 创建映射模型,新建File,  Core Data 选择Mappin ...

  4. SELF, self in CORE DATA

    Predicate SELF Represents the object being evaluated. CORE DATA Retrieving Specific Objects If your ...

  5. Core Data浅谈初级入门

    Core Data是iOS5之后才出现的一个框架,它提供了对象-关系映射(ORM)的功能,即能够将OC对象转化成数据,保存在SQLite数据库文件中,也能够将保存在数据库中的数据还原成OC对象.在此数 ...

  6. Core Data

    •   Core Data   是 iOS SDK   里的一个很强大的框架,允许程序员 以面向对象 的方式储存和管理数据 .使用 Core Data 框架,程序员可以很轻松有效 地通过面向对象的接口 ...

  7. iOS开发中的4种数据持久化方式【二、数据库 SQLite3、Core Data 的运用】

                   在上文,我们介绍了ios开发中的其中2种数据持久化方式:属性列表.归档解档.本节将继续介绍另外2种iOS持久化数据的方法:数据库 SQLite3.Core Data 的运 ...

  8. 我为什么用 SQLite 和 FMDB 而不用 Core Data

    凭良心讲,我不能告诉你不去使用Core Data.它不错,而且也在变好,并且它被很多其他Cocoa开发者所理解,当有新人加入你的组或者需要别人接手你的项目的时候,这点很重要.更重要的是,不值得花时间和 ...

  9. Core Data 概述

    Core Data是一个模型层的技术.Core Data帮助你建立代表程序状态的模型层.Core Data也是一种持久化技术,它能将模型对象的状态持久化到磁盘,但它最重要的特点是:Core Data不 ...

  10. 《驾驭Core Data》 第三章 数据建模

    本文由海水的味道编译整理,请勿转载,请勿用于商业用途.    当前版本号:0.1.2 第三章数据建模 Core Data栈配置好之后,接下来的工作就是设计对象图,在Core Data框架中,对象图被表 ...

随机推荐

  1. windows安装scrapy

    1.安装Twisted 直接pip install Twisted 然后报错 error: Microsoft Visual C++ 14.0 is required. Get it with &qu ...

  2. Unicode 和 ANSI

    Project Properties -> General-> Character set,里面显示了是不是unicode. Unicode处理String的方式不一样,一定要注意!!   ...

  3. django视图重定向

    # 原创,转载请留言联系 当请求访问到某个视图时,我们想让它重定向到其他页面,应该怎么做呢? 1.HttpResponseRedirect 需求:当我们访问127.0.0.1/my_redirect时 ...

  4. Appium+python自动化6-Remote远程控制【转载】

    前言 在第三篇启动app的时候有这样一行代码driver = webdriver.Remote('http://192.168.1.1:4723/wd/hub', desired_caps),很多小伙 ...

  5. vim的最最基本配置

    全部用户生效 /etc/vimrc 当前用户生效 ~/.vimr # 1.设置语法高亮syntax on # 2.显示行号 set nu # 3.设置换行自动缩进为4个空格 # 4.设置tab缩进为空 ...

  6. Android从相册选取视频

    1. /** * 从相册中选择视频 */ private void choiceVideo() { Intent i = new Intent(Intent.ACTION_PICK, android. ...

  7. AC日记——魔法少女LJJ bzoj 4399

    魔法少女LJJ 思路: 动态开点权值线段树+启发式合并: 来,上代码: #include <cmath> #include <cstdio> #include <cstr ...

  8. 如何让IE7,IE8支持css3

    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> 原理:在用ie浏览 ...

  9. Bean的实例化--静态工厂

    1,创建实体类User package com.songyan.demo1; /** * 要创建的对象类 * @author sy * */ public class User { private S ...

  10. 如何避免CSS :before、:after 中文乱码

    问题: 在进行页面开发时,经常会使用:before, :after伪元素创建一些小tips,但是在:before或:after的content属性使用中文的话,会导致某些浏览器上出现乱码. 解决方案: ...