高级UIKit-05(CoreData)
【day06_1_CoreDataPerson】:保存person对象到coreData数据库
保存大量数据时用CoreData保存到数据库,数据库会存在documents目录下
操作步骤:
1.创建空项目,勾上coreData
2.选中day06_1_CoreDataPerson.xcdatamo
添加entity实体,添加属性(attributes)
interger 16 int类型
interger 32 long类型
interger 16 long long类型
3.创建实体类,选择coreData选择最后一个,下一步。。。
4.创建storyboard,选择interface选中storyboard
5.选中项目名称选择main interface选中storyboard,删掉MXAppDelegate.m中创建window的代码,这时界面才能显示出来
添加数据到数据库:
// 创建appDelegate对象
MXAppDelegate *app = [UIApplication sharedApplication].delegate;
// 创建Person对象
Person *p = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:app.managedObjectContext];
p.name = @"李四";
p.age = [NSNumber numberWithInt:12];
从数据库查询数据:
// 创建appDelegate对象
MXAppDelegate *app = [UIApplication sharedApplication].delegate;
// 创建查询请求
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Person"];
// 执行查询请求 并返回结果
NSArray *personArray = [app.managedObjectContext executeFetchRequest:request error:nil];
// 输出
for (Person *p in personArray) {
NSLog(@"%@,%@",p.name,p.age);
}
删除数据:
// 创建appDelegate对象
MXAppDelegate *app = [UIApplication sharedApplication].delegate;
// 创建查询请求
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Person"];
// 执行查询请求 并返回结果
NSArray *personArray = [app.managedObjectContext executeFetchRequest:request error:nil];
for (Person *p in personArray) {
if ([p.name isEqualToString:@"张三"]) {
[app.managedObjectContext deleteObject:p]; // 删除数据
}
}
修改数据:
// 创建appDelegate对象
MXAppDelegate *app = [UIApplication sharedApplication].delegate;
// 创建查询请求
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Person"];
// 执行查询请求 并返回结果
NSArray *personArray = [app.managedObjectContext executeFetchRequest:request error:nil];
Person *p = personArray[0];
p.name = @"王五";
p.age = [NSNumber numberWithInt:23];
【day06_2_CoreDataTeam】:综合案例,使用数据库完成增删改查功能
添加和修改:
// 点击按钮后添加或修改数据
- (IBAction)clickedAction:(id)sender {
if (self.t) {
self.t.name = self.nameTF.text;
self.t.location = self.locationTF.text;
}else{
// 创建插入数据库对象
Team *team = [NSEntityDescription insertNewObjectForEntityForName:@"Team" inManagedObjectContext:self.app.managedObjectContext];
team.name = self.nameTF.text;
team.location = self.locationTF.text;
}
[self.app saveContext];
[self.navigationController popViewControllerAnimated:YES];
}
删除数据:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
Team *t = self.teamArray[indexPath.row];
[self.app.managedObjectContext deleteObject:t]; // 删除
[self.app saveContext]; // 保存
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
总结:使用数据库完成增删改要保存数据,查不需要。
综合练习:
游戏需求,
1.第一次运行直接进到添加用户的页面
2.从添加用户页面 选择某一个用户的话,返回首页,并显示欢迎这个用户
3.点击排行榜,跳转到新的页面,此页面显示每一个用户的最高分
4.用户列表页面可以进行删除和添加用户
5.将本次游戏得分和当前用户的最高分做比较如果这次高就覆盖得分
【PlayPlaneGame】:打飞机游戏
案例总结:如果要对从数据库取出的数组进行操作,应该让该数组为可变数组。
修改快捷键使用command+,打开,选择key bindings,修改
高级UIKit-05(CoreData)的更多相关文章
- Linux高级编程--05.文件读写
缓冲I/O和非缓冲I/O 文件读写主要牵涉到了如下五个操作:打开.关闭.读.写.定位.在Linux系统中,提供了两套API, 一套是C标准API:fopen.fclose.fread.fwrite.f ...
- Java入门 - 高级教程 - 05.网络编程
原文地址:http://www.work100.net/training/java-networking.html 更多教程:光束云 - 免费课程 网络编程 序号 文内章节 视频 1 概述 2 Soc ...
- iOS-数据持久化-CoreData
CoreData详解 介绍: 在Cocoa环境下,如果你想使用数据库(如sqlite),你可以使用sql语句的方式通过相关的工具类进行数据库的直接操作.当然你也可以通过别人封装之后的一些简单框架,使得 ...
- Playground 你不知道的小技巧, CoreData 的使用
Playground 的出现无疑是大大的提高了开发效率,可以节省大量的编译时间. 这里介绍在 Playground 中使用 CoreData 的小技巧. 我们新建一个工程 iOS 项目工程. 点击Fi ...
- [Swift]创建CoreData的两种方式
一.CoreData介绍 CoreData主要分为两部分: 上层是模型层,模型层有NSManagedObjectContext上下文管理着, 底层则是由SQLite实现的持久化部分,通过NSPersi ...
- swift3.0:CoreData的使用
一.介绍 CoreData不像slqite3那样编写代码繁琐,同时避免了使用了SQL语句的麻烦,也可以回避使用C语言的语法,降低了iOS开发的技术门槛. CoreData可降低开发成本,提高代码质量. ...
- Core Data数据操作
1.建立学生实体,插入100条数据 2.按条件查询学生数据 3.统计学生信息 4.修改学生信息 5.删除学生数据 import UIKit import CoreData class ViewCont ...
- CoreDataManager-Swift版-兼容iOS10以前的版本
import UIKit import CoreData // coredata管理器 class CoreDataManager: NSObject { // 单例 static let share ...
- iOS数据持久化-OC
沙盒详解 1.IOS沙盒机制 IOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文 ...
- Swift学习--微博的基础框架搭建
学习如何使用Swift写项目 一.搭建微博项目的主框架 1.1--搭建功能模块 1.2--在 AppDelegate 中的 didFinishLaunchingWithOptions 函数,设置启动控 ...
随机推荐
- HDU - 5009 Paint Pearls(dp+优化双向链表)
Problem Description Lee has a string of n pearls. In the beginning, all the pearls have no color. He ...
- 键盘码、ASCII码表
转载原文:http://www.cnblogs.com/knowledgesea/archive/2012/05/19/2508683.html ASCII码表 ASCII值 控制字符 ASCII值 ...
- jquery自定义分页插件
//每次只显示5个页码(function ($) { //设定页码方法,初始化 $.fn.setPager = function (options) { var opts = $.extend({}, ...
- [javascript]MooTools Selectors(MooTools 选择器) ELEMENT DOM选择
//ELEMENT DOM选择//on are tag names. //All the divs on the page: $$('div'); //All the divs and paragra ...
- ThinkPHP第四天(U函数,URL类型参数配置,伪静态后缀名配置,数据传递与获取$_GET等)
1.U('地址','参数','伪静态','是否跳转','是否显示域名'); 在模板中使用U方法而不是固定写死URL地址的好处在于,一旦你的环境变化或者参数设置改变,你不需要更改模板中的任何代码. 在模 ...
- Oracle 字段是多个值的字符串的查询处理
1.创建两张表一张用户表(T_User),一张兴趣小组表T_Group,其中小组成员字段存储用户ID列表以逗号隔开, 表:T_User 编号(F_ID) 名称(用户名) 1 张三 2 李四 3 王五 ...
- C语言回溯算法解决N皇后问题
回溯算法的模型是 x++, not satisfy ? x-- : continue. 代码中x作列号,y[x]保存第x列上皇后放置的位置. #include<stdio.h> #incl ...
- linux 进程通信
IPC: 管道,FIFO,信号,消息队列(system v/ posix),共享内存(system v/ posix),socket 同步机制: 互斥锁,条件变量,记录上锁, 信号量(system ...
- 更改firefox默认搜索引擎
使用Organizie Search Engines 这个插件 这个插件似乎不能添加搜索引擎 但是可以修改搜索引擎 好吧 由于有时候firefox会自动更新搜索引擎 所以不要对Google搜索引擎 ...
- BZOJ 1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典
题目 1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 401 Solv ...