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

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. jQuery的动画方法

    /* animate参数: 参数一:要改变的样式属性值,写成字典的形式 参数二:动画持续的时间,单位为毫秒,一般不写单位 参数三:动画曲线,默认为‘swing’,缓冲运动,还可以设置为‘linear’ ...

  2. Spring Mvc 传递参数要controller出现了400,日期参数全局处理,格式化yyyy-MM-dd 和yyyy-MM-dd HH:mm:ss

    描述:今天做一个业务操作的时候,ajax传递参数要controller出现了400,前后台都没有报错. 问题:springmvc 在接收日期类型参数时,如不做特殊处理 会出现400语法格式错误 解决: ...

  3. JavaScript中数组Array.sort()排序方法详解

    JavaScript中数组的sort()方法主要用于对数组的元素进行排序.其中,sort()方法有一个可选参数.但是,此参数必须是函数. 数组在调用sort()方法时,如果没有传参将按字母顺序(字符编 ...

  4. MySQL -MMM 学习整理

    一. 规划 1.主机规划 服务器 IP 作用 monitor 10.0.0.10 监控服务器 master-01 10.0.0.5 读写主机01 master-02 10.0.0.6 读写主机02 s ...

  5. 原来是adblock惹的祸

    一个在本地开发好的网站,放到服务器就不行了.花了好几个小时的时间,最后试着把adblock关了,然后正常了.

  6. 链式前向星BFS

    本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5399068 采用链式前向星的BFS: #include <iostream> #incl ...

  7. 替换Ubuntu默认的登录背景

    Ubuntu默认的登录背景看起来还是比较高贵的,但是作为一个爱折腾的人,当然要换成自己喜欢的背景图了.一开始跟着百度走,进了不少坑,最后还是自己走出来的.先上一个成品照 百度得到的答案几乎都是安装ub ...

  8. luogu P1072 Hankson的趣味题

    题目链接 luogu P1072 Hankson 的趣味题 题解 啊,还是noip的题好做 额,直接推式子就好了 \(gcd(x,a_0)=a_1=gcd(\frac{x}{a_1},\frac{a_ ...

  9. bzoj 5020: [THUWC 2017]在美妙的数学王国中畅游【泰勒展开+LCT】

    参考:https://www.cnblogs.com/CQzhangyu/p/7500328.html --其实理解了泰勒展开之后就是水题呢可是我还是用了两天时间来搞懂啊 泰勒展开是到正无穷的,但是因 ...

  10. android intent打开各种文件的方法

    android intent打开各种文件的方法   1./**  * 检测是否安装了某个软件  *   * @param pkgName "com.bill99.kuaishua" ...