Original address: http://blog.csdn.net/sxhong/article/details/8223881
Preference Settings Store
nsuserdefaults and the Settingbundle nsuserdefaults that it controls are used to save some settings that he will automatically write to when.
Nsubiquitouskeyvaluestore Multi-platform sync settings, limit size 64k, turn on entitlement, unique Apple ID (also consider the case when you can't connect to your icloud network)
File storage
I. Catalogue description
<application_home>/appname.app Bundle directory, including the program itself. Something inside will cause the signature to change and restart the program. itunes does not back up this directory after initial synchronization.
<application_home>/documents/stores user documentation and program data. You can make it visible through file sharing. itunes backup.
<application_home>/documents/inbox other programs require this program to open documents. Can be read and deleted, not added to the amendment. To change, you can move it first. itunes backup.
<application_home>/library/the root directory of the non-user data file store. Use one of the standard or custom folders to back up data that is not visible to the user. Do not apply this directory to store user data. itunes backup.
~library/application Support/<bundle_id> creates managed resources and data files for the user. Using this directory to store program state information, downloaded files even users create but agree to the data you manage. Save the file automatically.
~/library/caches/<bundle_id> a directory of files used to store cached files or programs that can be easily rebuilt.
<application_home>/tmp/temporary file directory, may be deleted by the system, should not be expected to always exist. Not backed up by itunes.
Other:
Offline data
can be downloaded or recreated, but users want to be able to access the data when they are offline. stored in <application_home>/documents or <application_home>/library/private Documents and marked as "Do not backup". The data in both locations is preserved when the storage space is low, and the Do not backup property blocks itunes or icloud backups. When an app no longer needs an offline data file, it should be deleted as soon as possible to avoid wasting the user's storage space. Set the do not back to property
- #include <sys/xattr.h>
- -(BOOL) Addskipbackupattributetoitematurl: (Nsurl *) URL
- {
- Const char* FilePath = [[URL path] filesystemrepresentation];
- Const char* attrname = "Com.apple.MobileBackup";
- u_int8_t attrValue = 1;
- int result = Setxattr (FilePath, Attrname, &attrvalue, sizeof (AttrValue), 0, 0);
- return result = = 0;
- }
Copy Code
Get directory Address
URLsForDirectory:inDomains:method returns the directory address in the form of Nsurl
Nssearchpathfordirectoriesindomains returns a directory address in the form of a string
Nshomedirectory return to the program root directory
Nstemporarydirectory Return temp file directory
Correlation variable Definition
- Nssearchpathdirectory
- enum {
- Nsapplicationdirectory = 1,//supported Applications (/applications)
- nsdemoapplicationdirectory,//unsupported applications and demonstration versions
- Nsdeveloperapplicationdirectory,//developer Applications (/developer/applications)
- Nsadminapplicationdirectory,//system and network administration applications
- Nslibrarydirectory,//various user-visible documentation, support, and configuration files (/library)
- Nsdeveloperdirectory,//developer Resources (/developer)
- Nsuserdirectory,//user Home Directories (/users)
- nsdocumentationdirectory,//
- nsdocumentdirectory,//
- Nscoreservicedirectory,//location of Core Services (system/library/coreservices)
- Nsautosavedinformationdirectory = 11,//location of user ' s autosaved documents Library/autosave information
- Nsdesktopdirectory = 12,//
- Nscachesdirectory = 13,//location of discardable cache files (library/caches)
- Nsapplicationsupportdirectory = 14,//location of application support files (library/application support)
- Nsdownloadsdirectory = 15,//
- Nsinputmethodsdirectory = 16,//
- Nsmoviesdirectory = 17,//
- Nsmusicdirectory = 18,//
- Nspicturesdirectory = 19,//
- Nsprinterdescriptiondirectory = 20,//
- Nssharedpublicdirectory = 21,//
- Nspreferencepanesdirectory = 22,//
- Nsitemreplacementdirectory = 99,//
- Nsallapplicationsdirectory = 100,//
- Nsalllibrariesdirectory = 101//
- };
- typedef Nsuinteger Nssearchpathdirectory;
Copy Code
- enum {
- Nsuserdomainmask = 1,//in User home directory
- Nslocaldomainmask = 2,//in the current machine
- Nsnetworkdomainmask = hosts visible in the 4,//network
- Nssystemdomainmask = 8,//system directory, non-modifiable (/system)
- Nsalldomainsmask = 0x0ffff,//All
- };
- typedef Nsuinteger NSSEARCHPATHDOMAINMASK;
Copy Code
two. Storage mode (i). Property list (plist)
Array,dictionary,data,string,nsnumber,nsdate Wait for NS objects to be written directly to the plist file to save.
(ii). Archive
implements the Nscoding protocol (Nscopy is also implemented?). Did not achieve also succeeded. ) Codec class description Nskeyedarchiver Create
-(ID) Initforwritingwithmutabledata: (Nsmutabledata *) data//archive is written to data in
Archives
+ ( NSData *) Archiveddatawithrootobject: (ID) rootobject//Archive to data in
+ (BOOL) Archiverootobject: (ID) rootobject ToFile: ( NSString *) path//Archive to file
-(void) finishencoding//After the call cannot continue to archive data, archive end must be called (public method archived)
–outputformat// Data encoding, XML, or binary
–setoutputformat:
encoded
–encodebool:forkey:
–encodebytes:length:forkey:
– Encodeconditionalobject:forkey:
–encodedouble:forkey:
–encodefloat:forkey:
–encodeint:forkey:
– Encodeint32:forkey:
–encodeint64:forkey:
–encodeobject:forkey:
Proxy delegate
–delegate
–setdelegate:
- –archiver:didencodeobject:
- –archiverdidfinish:
- –archiver:willencodeobject:
- –archiverwillfinish:
- –archiver:willreplaceobject:withobject:
Copy Code
Class and class name
+ Setclassname:forclass:
+ Classnameforclass:
–setclassname:forclass:
–classnameforclass:
Abnormal
extern NSString *nsinvalidarchiveoperationexception; Nskeyedunarchiver Create
–initforreadingwithdata:
Cancel Archive
+ (ID) unarchiveobjectwithdata: (NSData *) data//object to be archived from data
+ (ID) unarchiveobjectwithfile: (NSString *) path//obtained from file
Decoding data
-(BOOL) Containsvalueforkey: (NSString *) key//whether to include the given key encoding object
–decodeboolforkey:
–decodebytesforkey:returnedlength:
–decodedoubleforkey:
–decodefloatforkey:
–decodeintforkey:
–decodeint32forkey:
–decodeint64forkey:
–decodeobjectforkey:
-(void) finishdecoding//Notification delegate decoding ends and cannot continue decoding after call
Agent delegation
–delegate
–setdelegate:
- –unarchiver:cannotdecodeobjectofclassname:originalclasses:
- –unarchiver:diddecodeobject:
- –unarchiver:willreplaceobject:withobject:
- Finishing decoding
- –unarchiverdidfinish:
- –unarchiverwillfinish:
Copy Code
Class name
+ Setclass:forclassname:
+ Classforclassname:
–setclass:forclassname:
–classforclassname:
Abnormal
NSString *nsinvalidunarchiveoperationexception;
(iii). CoreData
Three attributes of entity in the Xcdatamodeld file:
Attributes: The corresponding interface variable for the Obj-c class.
The relationship between Relationships:entity, To-one,or To-many.
Fetched properties: Filtration of the above relationship? Reference 1 file storage format coredata_extern NSString * Const Nssqlitestoretype ns_available ( 10_4, 3_0);//SQLite form Storage
Coredata_extern NSString * Const Nsxmlstoretype ns_available (10_4, NA);//iOS not available
Coredata_extern NSString * Const Nsbinarystoretype ns_available (10_4, 3_0);//binary Form Storage
Coredata_extern NSString * Const Nsinmemorystoretype ns_available (10_4, 3_0);//Basic use procedure in memory (refer to SQL terminology in parentheses)
Nsmanagedobjectmodel object gets the template of the Xcdatamodeld file set in the project (that is, what tables are in your database, and what fields are in the table)
Nspersistentstorecoordinator object based on the above template generated or corresponding to a file (database files, specific data files)
Nsmanagedobjectcontext object above the operation of the file space, additions and deletions are carried out through here
Nsentitydescription object gets a table in the context above
Nsfetchrequest object to the above table of some specific operations, additions and deletions to check
-(Nsarray *) Executefetchrequest: (nsfetchrequest *) Request Error: (NSERROR * *) error; Use this method to execute the request above
If additions and deletions changed the context, remember to save. Example of an end procedure:
- Nsmanagedobjectcontext *managedobjectcontext = nil;
- Get the template
- Nsurl *modelurl = [[NSBundle mainbundle] urlforresource:@ "Your_xcdatamodeld_file_name" withExtension:@ "MOMD"];
- nsmanagedobjectmodel* Managedobjectmodel = [[Nsmanagedobjectmodel alloc] initwithcontentsofurl:modelurl];
- Specific documents, Document_dicrectory should be nsurl.
- Nsurl *storeurl = [@ "document_dicrectory" urlbyappendingpathcomponent:@ "Sqlite.sqlite"];
- Nserror *error = nil;
- Nspersistentstorecoordinator *coordinator = [[Nspersistentstorecoordinator alloc] Initwithmanagedobjectmodel: Managedobjectmodel];
- Using SQLite storage
- if (![ Coordinator Addpersistentstorewithtype:nssqlitestoretype Configuration:nil Url:storeurl Options:nil error:& ERROR]) {
- Error
- }
- Else
- {
- Get the operating space
- Managedobjectcontext = [[Nsmanagedobjectcontext alloc] init];
- [Managedobjectcontext Setpersistentstorecoordinator:coordinator];
- }
- Get a concrete table
- Nsentitydescription *entitydescription = [nsentitydescription
- entityforname:@ "One_entity_name"
- Inmanagedobjectcontext:managedobjectcontext];
- Action Statement
- Nsfetchrequest *request = [[Nsfetchrequest alloc] init];
- [Request Setentity:entitydescription];
- Conditions for manipulating statements
- Nspredicate *pred = [nspredicate predicatewithformat:@ "predicate_section"];
- [Request setpredicate:pred];
- Nsmanagedobject *manageobject = nil;
- Perform operations to get multiple specific data
- Nsarray *objects = [Managedobjectcontext executefetchrequest:request error:&error];
- if (objects = = nil) {
- Error
- }
- if ([objects Count] > 0)
- Multiple strips, take the first one.
- Manageobject = [Objects objectatindex:0];
- Else
- If there is no corresponding, create
- Manageobject = [Nsentitydescription
- insertnewobjectforentityforname:@ "table_name"
- Inmanagedobjectcontext:managedobjectcontext];
- Change the value of one of her fields
- [Manageobject setvalue:@ "VALUE" forkey:@ "one_field_name_of_the_table"];
- [Request release];
- Save, end
- [Managedobjectcontext save:&error];
Copy Code
(iv). Sqlite
Introduce the SQLite library, add the header file, and use the C API operation. The tedious part is that you can only store C data formats and need to be converted back and forth. Example of a procedure:
- Declaring a database
- Sqlite3 *database;
- Open it
- if (Sqlite3_open ("Database file path", &database)! = SQLITE_OK) {
- Sqlite3_close (database);
- Error
- }
- Char *errormsg;
- Execute no Return statement
- if (sqlite3_exec (database, "Delete and modify statements", NULL, NULL, &ERRORMSG)! = SQLITE_OK) {
- Sqlite3_close (database);
- Error
- }
- Sqlite3_stmt *statement;
- Execute Query statement
- if (SQLITE3_PREPARE_V2 (database, "query statement", -1, &statement, nil) = = SQLITE_OK) {
- Traverse results
- while (sqlite3_step (statement) = = Sqlite_row) {
- int row = Sqlite3_column_int (statement, 0);
- First column of data
- Char *rowdata = (char *) sqlite3_column_text (statement, 1);
- }
- End traversal
- Sqlite3_finalize (statement);
- }
- Close the database
- Sqlite3_close (database);
iOS Development data Store