tailieunhanh - iPhone SDK Programming A Beginner’s Guide phần 9

Phương pháp này có một đối tượng dữ liệu có khả năng ghi và trả về đối tượng lưu trữ như một id. Sau đó, bạn có thể viết các kho lưu trữ vào đĩa. Các bước để tạo và viết một kho lưu trữ vào đĩa như sau. Sau khi khởi tạo NSKeyedArchiver, mã hóa các đối tượng lưu trữ. | Chapter 15 Property Lists and Archiving 369 NSKeyedArchiver and NSKeyedUnarchiver The NSKeyedArchiver class archives objects while the NSKeyedUnarchiver class unarchives objects. NSKeyedArchiver NSKeyedArchiver stores one or more objects to an archive using the initForWritingWith MutableData method. To be archived an object must implement the NSCoding protocol. - id initForWritingWithMutableData NSMutableData data This method takes a writable data object and returns the archived object as an id. You can then write the archive to disk. The steps for creating and writing an archive to disk are as follows. First create an NSMutableData object. NSMutableData theData NSMutableData data After creating the data object create an NSKeyedArchiver passing the newly created data object as a parameter. NSKeyedArchiver archiver NSKeyedArchiver alloc initForWritingWithMutableData theData After initializing the NSKeyedArchiver encode the objects to archive. If you wish you can encode multiple objects using the same archiver provided all archived objects adopt the NSCoding protocol. The following code snippet illustrates. archiver encodeObject objectA forKey @ a archiver encodeObject objectB forKey @ b archiver encodeObject objectC forKey @ c archiver finishEncoding After archiving write the data object which now contains the archived objects to a file. theData writeToFile atomically YES NSKeyedUnarchiver You use NSKeyedUnarchiver to unarchive an archive. NSKeyedUnarchiver reconstitutes one or more objects from a data object that was initialized with an archive. To be unarchived an object must implement the NSCoding protocol. When programming for the iPhone you use the initForReadingWithData method. - id initForReadingWithData NSData data 370 iPhone SDK Programming A Beginner s Guide The steps to unarchive are similar to archiving. First create an NSData object from the previously archived file. NSData theData NSData dataWithContentsOfFile After .

crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.