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框架中,对象图被表 ...
随机推荐
- windows安装scrapy
1.安装Twisted 直接pip install Twisted 然后报错 error: Microsoft Visual C++ 14.0 is required. Get it with &qu ...
- Unicode 和 ANSI
Project Properties -> General-> Character set,里面显示了是不是unicode. Unicode处理String的方式不一样,一定要注意!! ...
- django视图重定向
# 原创,转载请留言联系 当请求访问到某个视图时,我们想让它重定向到其他页面,应该怎么做呢? 1.HttpResponseRedirect 需求:当我们访问127.0.0.1/my_redirect时 ...
- Appium+python自动化6-Remote远程控制【转载】
前言 在第三篇启动app的时候有这样一行代码driver = webdriver.Remote('http://192.168.1.1:4723/wd/hub', desired_caps),很多小伙 ...
- vim的最最基本配置
全部用户生效 /etc/vimrc 当前用户生效 ~/.vimr # 1.设置语法高亮syntax on # 2.显示行号 set nu # 3.设置换行自动缩进为4个空格 # 4.设置tab缩进为空 ...
- Android从相册选取视频
1. /** * 从相册中选择视频 */ private void choiceVideo() { Intent i = new Intent(Intent.ACTION_PICK, android. ...
- AC日记——魔法少女LJJ bzoj 4399
魔法少女LJJ 思路: 动态开点权值线段树+启发式合并: 来,上代码: #include <cmath> #include <cstdio> #include <cstr ...
- 如何让IE7,IE8支持css3
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> 原理:在用ie浏览 ...
- Bean的实例化--静态工厂
1,创建实体类User package com.songyan.demo1; /** * 要创建的对象类 * @author sy * */ public class User { private S ...
- 如何避免CSS :before、:after 中文乱码
问题: 在进行页面开发时,经常会使用:before, :after伪元素创建一些小tips,但是在:before或:after的content属性使用中文的话,会导致某些浏览器上出现乱码. 解决方案: ...