数据持久化And数据库

数据持久化 :

数据持久化,实际上就是将数据存放到网络或者硬盘上,这里是存储到本地的硬盘上,应用程序的本地硬盘是沙盒,沙盒实际上就是一个文件夹,它下面有4个文件夹。分别是Documents,Library,APP包和tmp文件夹,Documents里面主要是存储用户长期使用的文件,Library里面又有Caches和Preferences文件夹,Caches里面存放的是临时的文件,缓存。Preferences里面存放的是偏好设置。tmp里面也是临时的文件,不过和Caches还有区别,APP包里面是编译后的一些文件,包不能修改。

沙盒机制:有4个文件夹

Document :存储用户数据,需要备份到信息

Library/Caches :存储缓存文件,程序专用的支持文件

Library/Preferences :存储应用程序的偏好设置文件

.app :程序包(iOS8时,app不存储在沙盒中,有单独的文件夹储存所有程序的app包)不能被修改

tmp :存储临时文件。比如下载包,压缩后的再删除

获取主要目录路径的方式

NSHomeDirectory() : 沙盒主路径

NSDocumentDirectory : Documents文件夹

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSLibraryDirectory : Library文件夹

NSCachesDiectory  : Caches文件夹

NSTemporaryDirectory()  : tmp文件夹

Myapp.app : [[NSBundle mainBundle] bundlePath];

写入文件的方法

 字符串对象写入文件(NSString)

    //获取document路径

    NSArray * directories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString * docPath = [directories lastObject];

    NSString * strPath = [docPath stringByAppendingPathComponent:@"/text.txt"];

    NSString * content = @"xxx,赶紧把俯卧撑做了...";

    [content writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:nil];

数组对象写入文件 (NSArray)

    NSString * arrayPath = [docPath stringByAppendingPathComponent:@"array.txt"];

    NSArray * array = @[@"张三",@"李四",@"王五"];

    [array writeToFile:arrayPath atomically:YES];

字典对象写入文件 (NSDictionary)

    NSString * dicPath = [docPath stringByAppendingString:@"/dic.txt"];

    NSDictionary * dic = @{@"name":@"小黑",@"sex":@"男",@"age":@"22"};

    [dic writeToFile:dicPath atomically:YES];

二进制对象写入文件 (NSData)

    NSString * dataPath = [docPath stringByAppendingString:@"/data.txt"];

    UIImage * img = [UIImage imageNamed:@"1.png"];

    NSData * data = UIImagePNGRepresentation(img);

    [data writeToFile:dataPath atomically:YES];

NSFileManager :文件管理,使用detaultManager创建单例

//创建文件管理对象

    NSString * document = [docPath stringByAppendingString:@"/a/b/c"];

    NSFileManager * manager = [NSFileManager defaultManager];

    [manager createDirectoryAtPath:document withIntermediateDirectories:YES attributes:nil error:nil];

//创建文件text.txt

    NSString * filePath = [docPath stringByAppendingString:@"/a/b/c/text.txt"];

    [manager createFileAtPath:filePath contents:nil attributes:nil];

归档反归档

首先要遵循NSCoding协议,协议有2个方法

建一个类 3个属性

//归档是调用,对对象里面的属性/实例变量进行编码

- (void)encodeWithCoder:(NSCoder *)aCoder

{

    [aCoder encodeObject:_name forKey:@"name"];

    [aCoder encodeObject:_gender forKey:@"gender"];

    [aCoder encodeInt:_age forKey:@"age"];

}

//反归档是调用,对属性/实例变量进行编码

- (id)initWithCoder:(NSCoder *)aDecoder

{

    self = [super init];

    if (self) {

        self.name = [aDecoder decodeObjectForKey:@"name"];

        self.gender = [aDecoder decodeObjectForKey:@"gender"];

        self.age = [aDecoder decodeIntForKey:@"age"];

    }

    return self;

}

主程序中进行归档反归档

//1、定义NSMutable对象,用于存放归档后的二进制数据

    NSMutableData * mulData = [NSMutableData data];

    //2、定义归档工具类

    NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mulData];

    //3、对对象p进行编码(归档)

    [archiver encodeObject:p forKey:@"person"];

    //4、完成归档

    [archiver finishEncoding];

    //写入文件

    NSString * docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];

    NSString * dataPath = [docPath stringByAppendingPathComponent:@"archiveData.data"];

     [mulData writeToFile:dataPath atomically:YES];    

    //1、从文件中读取二进制数据

    NSData * readData = [NSData dataWithContentsOfFile:dataPath];

    //2、定义反归档工具类

    NSKeyedUnarchiver * unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:readData];

    //3、解码(反归档)

  Person * resultP = [unarchiver decodeObjectForKey:@"person"];

    //4、结束反归档

    [unarchiver finishDecoding];

- - - - - - - - - - - - - - - - - - - - - - - - - - -  - - -  - - -  - - - - - - - - -

数据库

SQL语句 增删改查

SQL中的常用关键字有

select、insert、update、delete、from、create、where、desc、order、by、group、table、alter、view、index等等

数据库中不可以使用关键字来命名表、字段

在ios项目中使用sqlite需要添加  libsqlite3.dylib 库

sqlite3_stmt    *stmt跟随指针

sqlite3_open()  打开数据库,没有数据库时创建

sqlite3_close()  关闭数据库文件

sqlite3_exec()  执行sql语句(不包括查询)int result = sqlite3_exec(db, sql.UTF8String, nil, nil, nil);

sqlite3_step()  在调用sqlite3_prepare后,使用这个函数在纪录集中移动。

还有一系列的函数,用于从纪录集字段中获取数据,如

sqlite3_column_text(), 取text类型的数据。
sqlite3_column_blob(),取blob类型的数据
sqlite3_column_int(), 取int类型的数据

/创建一张表  create table if not exists 表名 (字段名1 字段类型1, 字段名2 字段类型2, …) ;

NSString * ceateSQL = @"CRWATE TABLE IF NOT EXISTS PERSIONINFO(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, AGE INTEGER, SEX TEXT, WEIGHT INTEGER, ADDRESS TEXT)";"

/删除表  drop table if exists 表名 ;

/插入数据(insert) insert into 表名 (字段1, 字段2, …) values (字段1的值, 字段2的值, …) ; 

NSString * sql = [NSString stringWithFormat:@"insert into product values(%d,'%@',%f,'%@')",product.productID,product.productName,product.productPrice,product.expireDate];

/更新数据 (update)update 表名 set 字段1 = 字段1的值, 字段2 = 字段2的值, … ;

NSString * sql = [NSString stringWithFormat:@"update product set productName = '%@',productPrice = %f,expireDate = '%@' where productID = %d ",product.productName,product.productPrice,product.expireDate,product.productID];

/删除数据(delete) delete from 表名 ;

NSString * sql = [NSString stringWithFormat:@"delete from product"];//删除所有

NSString * sql = [NSString stringWithFormat:@"delete from product where productID = %d",productID];//删除某个

/查询数据 (select)select count (字段) from 表名 ;

NSString * sql = @"select * from product";//查询所有

NSString * sql = @"select * from product where productID = ?";//查询某个

关于stmt实际就是一个标示()执行语句[(sqlite3_exec)固定模式相当于3-6]

//1、打开数据库

    sqlite3 * db = [DB openDB];

    //2、准备sql语句

    NSString * sql = @"select * from product where productID = ?";

    //3、创建跟随指针

    sqlite3_stmt * stmt = nil;

    //4、判断sql语句是否正确

    int result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, nil);

    if (result == SQLITE_OK)

    {

        //sql语句正确

        //绑定'?'处的值

        sqlite3_bind_int(stmt, 1, productID);

        //5、获取返回结果

       int stepResult = sqlite3_step(stmt);

        Product * pro = [[Product alloc] init];

        if (stepResult == SQLITE_ROW)

        {

            //成功取出数据

            pro.productID = sqlite3_column_int(stmt, 0);

            pro.productName = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 1)];

            pro.productPrice = sqlite3_column_double(stmt, 2);

            pro.expireDate = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(stmt, 3)];

        }

        //6、释放stmt内存

        sqlite3_finalize(stmt);

        return [pro autorelease];

    }else

    {

        //sql语句不正确

        sqlite3_finalize(stmt);

    }

- - - - - - - - - - - - - - - - - - - - - - - 

集合视图 (UICollectionView)和UITableView很像

必须遵循这2个“代理”UICollectionViewDataSource,UICollectionViewDelegate

终于到UICollectionView的精髓了…这也是UICollectionView和UITableView最大的不同。UICollectionViewLayout可以说是UICollectionView的大脑和中枢,它负责了将各个cell、Supplementary View和Decoration Views进行组织,为它们设定各自的属性,包括但不限于

 

UICollectionViewDelegateFlowLayout这个代理是UICollectionView独有的

 

 

posted @ 2015-11-05 09:40  王云鹏  阅读(298)  评论(0)    收藏  举报