Store NSMutable Array into sqlite database

雨燕双飞 提交于 2019-12-24 09:37:24

问题


hi i am writng an iphone app and need a bit of help.. i have this uitableview which displays data that is contained in a nsmutable array.. this data is retrieved via some operations therefore amount of data is not the same every time...

what i want to know is if i had something like:

NSMutableArray *mArray;

how would i go about storing and loading the 'mArray' from an sqlite database..

Further info as requested:

@Caleb

@westsider

Array Contents: The contents of the array are simple module names (i.e subjects that a student learns on a course), that are retrieved from a .ics calendar file with some parsing operations.. i end up with an array that contains the data i want and then i display it using a tableview..

Basically i need to initially save the module names that are stored in the array so that the next time the user opens the application, the module names are still there..

Why Core Data When the user selects a cell in tableview (i.e. selects a module) i want to push another view controller that displays the module name where the user can add other data/strings that should be saved..


回答1:


What are the objects in the array? Are they all the same type? If they're dictionaries, do they all have the same keys? Are they made up of standard property list objects (NSDictionary, NSData, NSArray, NSString, NSNumber)? Why do you want to use an SQLite database rather than saving them some other way?

If you have an array of custom objects, the simplest thing to do is to implement NSCoding in the class (or classes) used in the array. Then, create an NSKeyedArchiver and archive your array using -archiveRootObject:toFile:.




回答2:


As long as everything in your array conforms to NSCoding you can serialize your array. Look at Archives and Serialization Programming Guide for creating and decoding archives.

You will need a column in your sqlite table that is of data type blob and use sqlite3_bind_blob to store it.

This link shows how to create an archive from an object. http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Archiving/Articles/creating.html#//apple_ref/doc/uid/20000949-BABGBHCA

This post shows how to retrieve a blob from a sqlite database. retrive-nsdata-from-sqlite-stored-in-blob-data-type-for-iphone-app



来源:https://stackoverflow.com/questions/5391820/store-nsmutable-array-into-sqlite-database

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