24_Core Data Demo
今天开始学习Core Data,类似于数据库,可以永久保存数据。不过当把App从iPhone删掉之后就没有了。可以用来保存App的运行数据。
Part1:
//
// ViewController.swift
// Core Data Demo
//
// Created by zcdll on 16/1/22.
// Copyright © 2016年 ZC. All rights reserved.
// import UIKit
import CoreData class ViewController: UIViewController { override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib. let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate let context: NSManagedObjectContext = appDel.managedObjectContext //第一次添加时使用,不注释会每次添加一次
/*
let newUser = NSEntityDescription.insertNewObjectForEntityForName("Users", inManagedObjectContext: context) newUser.setValue("zcdll", forKey: "username") newUser.setValue("asdf", forKey: "password") do { try context.save() } catch { print("Save problem") }
*/ let request = NSFetchRequest(entityName: "Users") do { let results = try context.executeFetchRequest(request) if results.count > 0 { for result in results as! [NSManagedObject] { print(result.valueForKey("username")!) print(result.valueForKey("password")!) } } } catch { print("Fetch failed") }
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Part2:
增加了更新数据,删除数据,查询数据等功能,感觉这样写很不灵活。
//
// ViewController.swift
// Core Data Demo
//
// Created by zcdll on 16/1/22.
// Copyright © 2016年 ZC. All rights reserved.
// import UIKit
import CoreData class ViewController: UIViewController { override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib. let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate let context: NSManagedObjectContext = appDel.managedObjectContext //第一次添加时使用,不注释会每次添加一次
/*
let newUser = NSEntityDescription.insertNewObjectForEntityForName("Users", inManagedObjectContext: context) newUser.setValue("zcdll12", forKey: "username") newUser.setValue("asdf12", forKey: "password") do { try context.save() } catch { print("Save problem") }
*/ let request = NSFetchRequest(entityName: "Users") //request.predicate = NSPredicate(format: "username = %@", "zcdll3") request.returnsObjectsAsFaults = false do { let results = try context.executeFetchRequest(request) if results.count > 0 { for result in results as! [NSManagedObject] { /* context.deleteObject(result)
//result.setValue("zcdll3",forKey: "username") do { try context.save() } catch {} */ if let username = result.valueForKey("username") as? String { print(username) } //print(result.valueForKey("password")!) } } } catch { print("Fetch failed") }
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
24_Core Data Demo的更多相关文章
- json data 解析demo
json data: demo: JsonObject jsonObject= JsonHandle.getAsJsonObject(city_dataInfo).get("data&quo ...
- vue双向数据绑定原理探究(附demo)
昨天被导师叫去研究了一下vue的双向数据绑定原理...本来以为原理的东西都非常高深,没想到vue的双向绑定真的很好理解啊...自己动手写了一个. 传送门 双向绑定的思想 双向数据绑定的思想就是数据层与 ...
- jQuery源码解读 - 数据缓存系统:jQuery.data
jQuery在1.2后引入jQuery.data(数据缓存系统),主要的作用是让一组自定义的数据可以DOM元素相关联——浅显的说:就是让一个对象和一组数据一对一的关联. 一组和Element相关的数据 ...
- SQOOP Load Data from Oracle to Hive Table
sqoop import -D oraoop.disabled=true \ --connect "jdbc:oracle:thin:@(description=(address=(prot ...
- jquery data方法
jquery.data()文档:http://api.jquery.com/jQuery.data/ html5有个data-*属性,跟这个功能一样. Note: This is a low-leve ...
- 代码中函数、变量、常量 / bss段、data段、text段 /sct文件、.map文件的关系[实例分析arm代码(mdk)]
函数代码://demo.c #include<stdio.h> #include<stdlib.h> , global2 = , global3 = ; void functi ...
- jQuery数据缓存$.data 的使用以及源码解析
一.实现原理: 对于DOM元素,通过分配一个唯一的关联id把DOM元素和该DOM元素的数据缓存对象关联起来,关联id被附加到以jQuery.expando的值命名的属性上,数据存储在全局缓存对象jQu ...
- Node中的Socket.IO 简单Demo及说明
注:下面Demo的Server和Client都是纯后端. 并没有web页面. Server端代码: var express = require('express'); var app = expres ...
- Faster RCNN算法demo代码解析
一. Faster-RCNN代码解释 先看看代码结构: Data: This directory holds (after you download them): Caffe models pre-t ...
随机推荐
- phpExcel在封装
<?php /** * 数组生成Excel * @author zouhao zouhao619@gmail.com * 使用示例 * $excel =new Excel(); $data=ar ...
- 利用Telnet来模拟Http请求 有GET和POST两种
利用Telnet来模拟Http请求---访问百度. 1.打开"运行"->cmd进入命令环境: 2.输入"telnet www.baidu.c ...
- Ghost Button制作教程及设计趋势分析
概述:Ghost Button(虚拟按钮)是网页设计中一个非常实用的按钮样式,特别是图片背景中,有出色的效果.今天我们一起来研究Ghost Button的各种效果的制作方法,并对Ghost Butto ...
- JMS详细的工作原理【转】
如果手机只能进行实时通话,没有留言和短信功能会怎么样?一个电话打过来,正好没有来得及接上,那么这个电话要传递的信息肯定就收不到了.为什么不能先将信息存下来,当用户需要查看信息的时候再去获得信息呢?伴随 ...
- nginx install lua module
#install luajit #http://luajit.org/download.html .tar.gz cd LuaJIT- make install PREFIX=/home/allen. ...
- php框架
使用composer构建的php框架 github: https://github.com/Ev2le0/LeoFramework 实现功能: 1)路由 2)ORM 3)视图
- [LeetCode]题解(python):139-Word Break
题目来源: https://leetcode.com/problems/word-break/ 题意分析: 给定一个字符串s和一个字典dict,判断s是不是由字典dict里面的元素组成的. 题目思路: ...
- MYSQL @、@@、@x
说明一: @var这种形式表示的是用户自定义的变量. 说明二: @@var这种形式表示的是系统变量.它又可以分为两种一种是全局(@@global)的一种是会话(@@session)的. 说明三: MY ...
- getline函数
在我的印象中,getline函数常常出如今自己的视野里,模糊地记得它常常常使用来读取字符串 .可是又对它的參数不是非常了解,今天又用到了getline函数,如今来细细地总结一下: 首先要明确设计get ...
- mybatis-generator生成model和dao层代码
.建立文件夹myibatisGen 2.下载mybatis-generator-core-1.3.1.jar或者其它版本的jar包,到myibatisGen文件夹下 3.为生成代码建立配置文件“gen ...