Serialization of IOS WriteToFile and objects

Source: Internet
Author: User
Tags object serialization

Preface: Do a picture to browse the small demo, support arbitrarily add, delete the picture, the picture enlarges, shrinks, with the rectangle box. Subsequent blogs will explain in detail the various issues encountered during this process. This article is mainly about, in doing add, remove this function, encountered the problem of saving files.

We want to achieve in the future every time you open this program, do not have to add a manual, and will retain the last image that the user has selected, then you need to put these pictures in memory, every time read from memory.

Body:

There are several ways to save a file?

Write directly to file, object serialization

Second, write directly to file

In my program, I have a nsmutabledictionary of images, each of which corresponds to a key.

1, directly call Nsdictionary's writetofile:atomically: method, the DIC into a specified directory.

-(BOOL) WriteToFile: (NSString *) path atomically: (BOOL) flagdescription    

Function: Writes a property list representation of the contents of the dictionary to a given path.

Writes a property list representing this dictionary content to the specified path.

Problem: Directory creation is not a problem, but has been writetofile write failed??

Analysis Ideas: Query method Comments, there is a passage:

This method recursively validates the contained objects is property list objects (instances of NSData, NSDate, N Snumber, NSString, Nsarray, or nsdictionary) before writing out of the file, and returns NO if all the objects is not proper Ty list objects, since the resultant file would not being a valid property list.

This method recursively verifies that all contained objects are objects in the property list (including NSData, NSDate, NSNumber, NSString, Nsarray, or nsdictionary) before the DIC is written to the file.

If all the objects in DIC are not all objects in the property list, return no because the last generated file is not a valid property list.

Reason for failure: because the obj in my dic is uiimage and does not belong to the property list, it fails to return.

Solution: Replace the UIImage with the object in the property list NSData, put NSData into DIC.

NSData *data = Uiimagepngrepresentation (IMG);  

2. Remove from File

In WriteToFile's note, write to: If The dictionary ' s contents is all property list objects, the file written by this method can is use D to initialize a new dictionary with the class method Dictionarywithcontentsoffile:or the instance method initwithconten Tsoffile:.

If this dic content is a property list, then the file written by this method can be used to initialize a new dictionary, Method: Dictionarywithcontentsoffile: or Initwithcontentsoffile:.

Nsmutabledictionary *dic= [Nsmutabledictionary Dictionarywithcontentsoffile:_filepath];

Problem: When I call this dictionarywithcontentsoffile after a successful write, I get the new dic always having problems.

Analysis Reason: Write problem? parameter _filepath problem? Write if there is a problem, then it will not succeed, a new path to succeed, it is _filepath problem. Enter PO _filepath in the failed print box, which is a null pointer. The question is how I never thought of it. I set the _filepath as a global variable, and in the first Init method, I gave it a value, but how can it be nil here??

Take a look at my assignment method:

_filepath = [NSString stringwithformat:@ "%@/documents/imageviews/test.plist", Nshomedirectory ()];

Now you know where the problem is?!! In the previous blog detailed analysis of this problem, no use of alloc and other methods to construct the object, if in the other parts of the program to use, must add retain!!!
Modify everything after ok!

III. serialization of objects

When it comes to new content, let's review the Java object serialization first. In Java, objects that need to be serialized must implement the serializable interface to store and read objects by calling WriteObject, ReadObject.

In OC, it is similar. Objects that need to be serialized must implement the Nscoding protocol and override the Encodewithcoder and Initwithcoder two methods for encoding and deserializing, respectively, by invoking Nskeyedarchiver, Nskeyedunarchiver these two classes to store and read objects. If the inherited class already implements the Nscoding protocol, then the subclass is not implemented.

[Nskeyedarchiver archiverootobject:obj tofile:filename];   serialization of obj = [Nskeyedunarchiver unarchiveobjectwithfile:filename];   Deserialization

Because there is no attempt at the moment, just a brief introduction, when used to study in detail. Refer to: http://blog.csdn.net/holydancer/article/details/7371643


Iv. Summary

1, WriteToFile method, only applicable to NSData, NSDate, NSNumber, NSString, Nsarray, or nsdictionary these classes.

2, our custom class to write to the file, is the serialization method.

Serialization of IOS WriteToFile and objects

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.