IOS development-data storage NSCoder

Source: Internet
Author: User

IOS development-data storage NSCoder
One problem that cannot be solved in software is data storage. in PC, data is usually stored in the database. If iOS is used with the backend, therefore, you do not need to consider the data storage issue. The plist storage was written last time, but the data is stored with some simple key-value pairs. This time, we need to store some defined types in plist, for example, images. In this case, we can use the NSCoding protocol to store data in plist files in the form of similar files, then read data from plist files. NSCoder will be used when the protocol is used. If there is no concept of archive and decompression, it can be simply understood as data serialization and deserialization. The basic concept NSCoding is a protocol. if NSCoding is implemented, You need to implement two methods:-(void) encodeWithCoder :( NSCoder *) ACO;-(id) initWithCoder :( NSCoder *) aDecoder; // The main parameter in the NS_DESIGNATED_INITIALIZER method is NSCoder, which is the abstract class of the archivie byte stream. you can write data to a coder or read the data we write from the coder. NSCoder is an abstract class and cannot be used directly to create objects. however, you can use its subclass NSKeyedUnarchiver to read data from the byte stream, and NSKeyedArchiver writes the object to the byte stream. This article takes books as an example: create a Book class, Book. code in h: # import <Foundation/Foundation. h> # import <UIKit/UIKit. h> @ interface Book: NSObject <NSCoding> @ property (strong, nonatomic) UIImage * ConverPicture; @ property (strong, nonatomic) NSString * BookName; @ property (strong, nonatomic) NSString * Author; @ property (strong, nonatomic) NSNumber * Price; @ endBook. in m, the NSCoding method is implemented. Note that the UIImage method is different from other methods: @ implementation Book-(void) enc OdeWithCoder :( NSCoder *) aCoder {// note that the jpgimage is stored here and the call [ecoder encodeObject: UIImageJPEGRepresentation (self. converPicture, 1.0) forKey: @ "ConverPicture"]; [ecoder encodeObject: _ BookName forKey: @ "BookName"]; [ecoder encodeObject: _ Author forKey: @ "Author"]; [ecoder encodeObject: _ Price forKey: @ "Price"];}-(id) initWithCoder :( NSCoder *) aDecoder {self. converPicture = [UIImage imageWithData: [aDecoder decodeObjec TForKey: @ "ConverPicture"]; self. bookName = [aDecoder decodeObjectForKey: @ "BookName"]; self. author = [aDecoder decodeObjectForKey: @ "Author"]; self. price = [aDecoder decodeObjectForKey: @ "Price"]; return self ;}@ endDemo if this is normal, you do not need to create a page. However, you need to demonstrate the effect of UIImage, Main. layout in storyboard: to explain a little, the first two are saved single files, and the last two are saved multi-files. UIImage displays the stored image: ViewController defined field: @ property (strong, nonatomic) NSString * storagePath; @ property (stron G, nonatomic) NSString * storageListPath; @ property (strong, nonatomic) NSMutableArray * bookList; set the path. If it is not clear, refer to the previous blog: NSArray * codepath = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); _ storagePath = [codepath [0] stringByAppendingPathComponent: @ "book. plist "]; NSLog (@" % @ ", NSHomeDirectory (); _ storageListPath = [codepath [0] stringByAppendingPathComponent: @" booklist. Plist "]; single archive: Book * book = [[Book alloc] init]; UIImage * image = [UIImage imageNamed: @" Code1.jpg "]; book. converPicture = image; book. bookName = @ "Hundred Years of loneliness"; book. author = @ "Garcia. marquez "; book. price = [[NSNumber alloc] initWithInteger: 45]; if ([NSKeyedArchiver archiveRootObject: book toFile: _ storagePath]) {NSLog (@ "");} decompress one: book * decodeBook = [NSKeyedUnarchiver unarchiveObjectWithFile: _ storagePath]; self. myImag EView. image = decodeBook. converPicture; NSLog (@ "% @", decodeBook. converPicture); NSLog (@ "% @", decodeBook. bookName); NSLog (@ "ARCHIVE solved successfully"); multiple archives: self. bookList = [NSMutableArray array]; for (NSInteger I = 1; I <3; I ++) {Book * book = [[Book alloc] init]; NSString * imageName = [NSString stringWithFormat: @ "Code%ld.jpg", (long) I]; UIImage * image = [UIImage imageNamed: imageName]; book. converPicture = image; book. bookName = [NSStri Ng stringWithFormat: @ "Hundred Years of loneliness % ld", (long) I]; book. author = [NSString stringWithFormat: @ "Garcia. marquez % ld ", (long) I]; book. price = [[NSNumber alloc] initWithInteger: 45]; [self. bookList addObject: book];} if ([NSKeyedArchiver archiveRootObject: self. bookList toFile: _ storageListPath]) {NSLog (@ "");} multiple archive: self. bookList = [NSKeyedUnarchiver unarchiveObjectWithFile: _ storageListPath]; Book * nextBook = self. bookList [1]; self. myImageView. image = nextBook. converPicture; NSLog (@ "" "); the Code basically finds that archiving and decompression are very simple, but the disadvantages of this method are obvious, in this way, you can only archive, save, and decompress data at a time. It is easier to use a small amount of data. If you want to modify one of the items when there is too much data, it is time-consuming to decompress the entire data and archive the entire data.

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.