first core data
//
// 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的更多相关文章
- iOS之Core Data及其线程安全
一.简介 Core Data是iOS5之后才出现的一个框架,它提供了对象-关系映射(ORM)的功能,即能够将OC对象转化成数据,保存在SQLite数据库文件中,也能够将保存在数据库中的数据还原成OC对 ...
- Core Data的一些常见用法
一.简介 Core Data是一个纯粹的面向对象框架,其本质就是一个ORM(对象关系映射:Object Relational Mapping),能以面向对象的方式操作SQLite数据库.在实际开发中绝 ...
- Core Data 使用映射模型
Core Data 使用映射模型 如果新版本的模型存在较复杂的更改,可以创建一个映射模型,通过该模型指定源模型如何映射到目标模型. 创建映射模型,新建File, Core Data 选择Mappin ...
- SELF, self in CORE DATA
Predicate SELF Represents the object being evaluated. CORE DATA Retrieving Specific Objects If your ...
- Core Data浅谈初级入门
Core Data是iOS5之后才出现的一个框架,它提供了对象-关系映射(ORM)的功能,即能够将OC对象转化成数据,保存在SQLite数据库文件中,也能够将保存在数据库中的数据还原成OC对象.在此数 ...
- Core Data
• Core Data 是 iOS SDK 里的一个很强大的框架,允许程序员 以面向对象 的方式储存和管理数据 .使用 Core Data 框架,程序员可以很轻松有效 地通过面向对象的接口 ...
- iOS开发中的4种数据持久化方式【二、数据库 SQLite3、Core Data 的运用】
在上文,我们介绍了ios开发中的其中2种数据持久化方式:属性列表.归档解档.本节将继续介绍另外2种iOS持久化数据的方法:数据库 SQLite3.Core Data 的运 ...
- 我为什么用 SQLite 和 FMDB 而不用 Core Data
凭良心讲,我不能告诉你不去使用Core Data.它不错,而且也在变好,并且它被很多其他Cocoa开发者所理解,当有新人加入你的组或者需要别人接手你的项目的时候,这点很重要.更重要的是,不值得花时间和 ...
- Core Data 概述
Core Data是一个模型层的技术.Core Data帮助你建立代表程序状态的模型层.Core Data也是一种持久化技术,它能将模型对象的状态持久化到磁盘,但它最重要的特点是:Core Data不 ...
- 《驾驭Core Data》 第三章 数据建模
本文由海水的味道编译整理,请勿转载,请勿用于商业用途. 当前版本号:0.1.2 第三章数据建模 Core Data栈配置好之后,接下来的工作就是设计对象图,在Core Data框架中,对象图被表 ...
随机推荐
- spark streaming 异常No output streams registered, so nothing to execute
实现spark streaming demo时,代码: public static void main (String[] args) { SparkConf conf = new SparkConf ...
- Postman前端HTTP请求调试神器教程
Postman功能: 主要用于模拟网络请求包 快速创建请求 回放.管理请求 快速设置网络代理 我们看下界面: 一 接口请求流程: 二 postman使用 从流程图中我们可以看出,一个接口请求需要设 ...
- 关于JAVA SESSION的小测试
手生就要多练啊... package com.jeelearning.servlet; import java.io.IOException; import java.io.PrintWriter; ...
- (1)C# 创建ef sqlserver
连接sql 如果报错不能连接的错误 把这三个IP地址的端口号设置上,并启用.第一个18.6是本机ip,之后就可以测试了 最后重启服务器
- 深入分析CVE-2016-5195 Dirty Cow
前面一段时间,这个编号为CVE-2016-5195的漏洞刷爆了各个安全相关的博客和网站,这个漏洞可以对任意可读文件进行写操作从而导致提权,通杀了包括Android在内的绝大多数linux版本,,影响不 ...
- #请用索引取出下面list的指定元素:
#!/usr/bin/python # -*- coding: utf-8 -*- L = [ ['Apple', 'Google', 'Microsoft'], ['Java', 'Python', ...
- bzoj 3462: DZY Loves Math II
3462: DZY Loves Math II Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 211 Solved: 103[Submit][Sta ...
- Oracle多用户对一个表进行并发插入数据行操作
oracle数据库支持多用户间同时对同一个表进行操作,但是数据不一定同步,因为oracle数据库是支持脏数据的,比如A用户删除了表的数据但没有提交,B用户也能查询访问到,如果要避免这种情况只能加锁,A ...
- JMeter之Ramp-up Period(in seconds)说明(可同时并发)(转载)
Ramp-up Period(in seconds) [1]决定多长时间启动所有线程.如果使用10个线程,ramp-up period是100秒,那么JMeter用100秒使所有10个线程启动并运行. ...
- 【Maven】1.使用myecplise配置自己的Maven配置,不使用默认的maven
[好文章]参考地址: http://www.cnblogs.com/luotaoyeah/p/3764533.html ---------------------------------------- ...