Storing NSMutableArray in Sqlite

拥有回忆 提交于 2019-12-01 11:37:13

Ok--this is a bit easier with Core Data, but the basics are the same. You need to store the array data into an NSData object using an archive:

NSData *dataFromArray = [NSKeyedArchiver archivedDataWithRootObject:myArray];

And then save that data as a blob in the database. Depending on the SQLite wrapper you use, it may have a method taking the NSData object and translating it into bytes for you, otherwise you can get the raw bytes with:

const char *arrayBytes = [dataFromArray bytes];

To get the data back out, you get the bytes from the database back into an NSData object and then unarchive it. Assuming that you can get your blob out of the database and into an NSData object called dataFromDB, you will do:

NSArray *myArrayFromDB = [NSKeyedUnarchiver unarchiveObjectWithData:dataFromDB];

And you'll have your array back!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!