青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

逛奔的蝸牛

我不聰明,但我會很努力

   ::  :: 新隨筆 ::  ::  :: 管理 ::
@import url(http://m.shnenglu.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
Saving User Data 

Disk Icon
In a nutshell: saving data created by the user

Most applications need a way to save data to disk and bring it back later. In many cases, the best built-in class to use for this is NSKeyedArchiver. It doesn't provide the scalability of a SQL database, but it is incredibly easy to use.

In this tutorial, you'll add saving support to the MailDemo application, which was originally used in the Cocoa Bindings tutorial. It's not necessary to read that tutorial first, but you do need to understand Objective-C and general Cocoa concepts.

Download the project so you can follow along and make the changes as they're described. Note that this is not the finished project, just the starting point. Mac OS X 10.3 (Panther) is required to use this project.

Download the MailDemo Xcode project 56k

Most Cocoa applications are typically either "regular" or multi-document. TextEdit, for example, is a multi-documentation application, whereas AddressBook is not. This tutorial does not discuss multi-document applications, though the concepts are very similar.

1 of 9

NSKeyedArchiver in a Nutshell

The NSKeyedArchiver class accepts data objects from your application and stores them on disk. It stores the objects in a file as binary data.

Encoding Diagram

You can open a binary data file in a text editor, but it's quite hard to make any sense of the contents that way. Binary files are designed to be easily read by the computer, not a person. As a result, binary files take up less space on disk than plain text files and can be loaded by an application very quickly.

Interface Builder, for example, typically stores NIB files in a binary format.

2 of 9

Adding Saving Support to Mailbox

The process of converting objects into binary data is called encoding. Your job is to tell Cocoa how your data should be encoded. Here's the code to add to Mailbox.m to do that:

Mailbox.m
- (void) encodeWithCoder: (NSCoder *)coder { [coder encodeObject: properties forKey:@"properties"]; [coder encodeObject: emails forKey:@"emails"]; }

This method adds the Mailbox's data to the NSCoder object that's passed in, using the-encodeObject:forKey: message to store each item. You can choose whatever names you like for the keys, but they must mirror those used in -initWithCoder.

The -initWithCoder: method is called when data is being loaded back from disk. Here's the version of that method to put in Mailbox.m:

- (id) initWithCoder: (NSCoder *)coder { if (self = [super init]) { [self setProperties: [coder decodeObjectForKey:@"properties"]]; [self setEmails: [coder decodeObjectForKey:@"emails"]]; } return self; }

Finally, change Mailbox.h to indicate that the Mailbox class now conforms to the NSCodingprotocol:

Mailbox.h
@interface Mailbox : NSObject <NSCoding>

That's all we need to do in this file.

3 of 9

Adding Saving Support to Email

Here's the code to add to Email.m to enable saving for the Email class:

Email.m
- (void) encodeWithCoder: (NSCoder *)coder { [coder encodeObject: properties forKey:@"properties"]; }

Here's the code to enable loading from disk:

- (id) initWithCoder: (NSCoder *)coder { if (self = [super init]) { [self setProperties: [coder decodeObjectForKey:@"properties"]]; } return self; }

This code is nearly identical to the methods added to Mailbox. The difference is that there's one less instance variable to store.

Just as before, add NSCoding to Email.h:

Email.h
@interface Email : NSObject <NSCoding>

We're now done with this file.

4 of 9

Declare New Methods in MyController.h

We'll create three new methods to MailDemo's MyController class to support saving. Add these declarations to the MyController.h file:

MyController.h
- (NSString *) pathForDataFile; - (void) saveDataToDisk; - (void) loadDataFromDisk;

The first method returns the destination path of the data file. The other two methods are used to save and load the data, respectively.

5 of 9

Choosing the File Location

Before an application can save, the developer must select a location for the data. Mac OS X applications generally store such things in ~/Library/Application Support/<Application>/. The application must create that directory if it doesn't already exist.

Here's the code to add to MyController.m to do all of this:

MyController.m
- (NSString *) pathForDataFile { NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *folder = @"~/Library/Application Support/MailDemo/"; folder = [folder stringByExpandingTildeInPath]; if ([fileManager fileExistsAtPath: folder] == NO) { [fileManager createDirectoryAtPath: folder attributes: nil]; } NSString *fileName = @"MailDemo.cdcmaildemo"; return [folder stringByAppendingPathComponent: fileName]; }

On the first line of this method, we set up an instance of NSFileManager. Many of the file-related tasks that you would typically do by hand with the Finder can be accomplished using NSFileManager.

Next, we create an NSString named folder, and set it to the path of the destination folder.

The next line is a bit tricky in that it re-assigns the folder variable to a different version of the same string. The -stringByExpandingTildeInPath method returns a version of the folder path with the tilde replaced by the user's home directory, such as "/Users/scott".

On the next line, we ask NSFileManager if the folder already exists. If it doesn't, we request that it is created by sending the -createDirectoryAtPath:attributes: message.

Finally, we append the file name to the folder path and return the complete path.

6 of 9

Archiving the Data

Now that you have added the NSCoding methods to the data classes, you can ask NSKeyedArchiverto write your objects to disk. You only need to tell the archiver about your top-level objects. In this case, the term "top level" just means the objects which are listed as instance variables in MyController.h.

The only top-level object for the MyController class is the "mailboxes" array. Here's the code to add to MyController.m to save the array (and everything it contains) to disk:

MyController.m
- (void) saveDataToDisk { NSString * path = [self pathForDataFile]; NSMutableDictionary * rootObject; rootObject = [NSMutableDictionary dictionary]; [rootObject setValue: [self mailboxes] forKey:@"mailboxes"]; [NSKeyedArchiver archiveRootObject: rootObject toFile: path]; }

On the first line, we retrieve the destination file path. After that, we create an NSMutableDictionary and place the mailboxes array in it. If you have other top-level objects, add them to this dictionary.

This dictionary is considered the "root object" -- a master object which contains a tree of all other data objects. We pass this dictionary to NSKeyedArchiver in the +archiveRootObject:toFile: class method.

7 of 9

How Archiving Works

Once NSKeyedArchiver receives the +archiveRootObject:toFile: message, it recursively calls-encodeWithCoder on the entire tree of objects inside the root object. Each instance of Mailbox and Email adds its own bit of data to the archiver.

Archiving Diagram

After all of the objects have added their encoded data, the archiver automatically creates an NSData object and writes its contents to the file path we supplied.

8 of 9

Reloading Data at Startup

Now we will implement a method in MyController.m to load the data from disk:

MyController.m
- (void) loadDataFromDisk { NSString * path = [self pathForDataFile]; NSDictionary * rootObject; rootObject = [NSKeyedUnarchiver unarchiveObjectWithFile:path]; [self setMailboxes: [rootObject valueForKey:@"mailboxes"]]; }

This is essentially the reverse of the saving method. Once again, we retrieve the path to the file and create a dictionary which will serve as the root object.

This time, though, we're talking to the NSKeyedUnarchiver class, which is similar toNSKeyedArchiver, but specializes in loading data from disk. We send it the-unarchiveObjectWithFile message, which will read the data file, un-encode the contents, and return the root object.

After that, it's just a matter of using -setMailboxes to repopulate the mailboxes array with the saved data.

9 of 9

Automating Save and Reload

MailDemo can now save and reload data, but the process should ideally be transparent to the user. We only need to add a few lines of code to MyController.m to make that happen:

MyController.m
- (void) awakeFromNib { [NSApp setDelegate: self]; [self loadDataFromDisk]; }

The -setDelegate message registers MyController to be the application delegate. It's possible to do this in Interface Builder as well.

The next line of code simply calls -loadDataFromDisk. Since MailDemo uses Cocoa bindings, the table views are automatically updated after the data is loaded.

To complete the cycle, we need to make sure the data is saved when the application quits. To do this, add the -applicationWillTerminate delegate method show below:

- (void) applicationWillTerminate: (NSNotification *)note { [self saveDataToDisk]; }

We're all set! Build and run the project. You should see that your data stays intact between sessions. If you encountered any difficulties, go back and repeat the steps above.


Wrap Up

We've added NSCoding support to the data classes and saving support to the application. Implementing encoding also sets your application up to support copy/paste and drag-and-drop. Both are typically accomplished by writing encoded objects to the pasteboard.

Download the Finished MailDemo Xcode project 57k

As always, let us know what you think about the tutorial.


Further Reading

Local Page Intro to Cocoa Bindings the original tutorial which used MailDemo
Apple Archives and Serialization Apple's official documentation for archiving
Remote Site Cocoa Dev Central blog author's personal site
@import url(http://m.shnenglu.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
posted on 2011-12-02 00:08 逛奔的蝸牛 閱讀(550) 評論(0)  編輯 收藏 引用 所屬分類: Cocoa
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            欧美影院在线| 老司机67194精品线观看| 午夜一级久久| 亚洲精品视频一区| 亚洲国产精品久久久久婷婷老年| 欧美视频在线一区| 国产精品久久久久久久久久妞妞| 欧美肉体xxxx裸体137大胆| 欧美性大战久久久久久久蜜臀| 欧美日韩国产色综合一二三四 | 亚洲性感美女99在线| 亚洲精品在线二区| 中文欧美字幕免费| 久久精品人人做人人爽电影蜜月| 免费日韩成人| 国产精品免费看久久久香蕉| 国内久久精品视频| 99精品免费视频| 欧美一区午夜视频在线观看| 开元免费观看欧美电视剧网站| 亚洲福利免费| 亚洲午夜一级| 欧美 日韩 国产精品免费观看| 欧美日韩中国免费专区在线看| 国产美女扒开尿口久久久| 亚洲国产成人精品视频| 亚洲一区二区三区影院| 久久视频在线视频| 99在线精品视频| 久久久999| 国产精品亚洲精品| 日韩视频免费在线| 久久精品一区二区| 亚洲精选中文字幕| 久久亚洲免费| 国产亚洲一区二区三区| 99在线精品观看| 女生裸体视频一区二区三区| 亚洲一区二区三区在线看| 蜜桃久久精品乱码一区二区| 国产欧美日韩免费看aⅴ视频| 99国产精品视频免费观看| 蜜臀91精品一区二区三区| 亚洲欧美福利一区二区| 欧美日韩直播| 亚洲伊人网站| 夜夜嗨av色综合久久久综合网| 欧美77777| 亚洲高清精品中出| 麻豆成人在线播放| 久久国产精品99久久久久久老狼 | 亚洲国产精品成人一区二区| 国产酒店精品激情| 一区二区不卡在线视频 午夜欧美不卡在 | 亚洲精品小视频| 久久免费少妇高潮久久精品99| 国产精品一二三| 在线视频精品| 91久久极品少妇xxxxⅹ软件| 久久这里有精品15一区二区三区| 国产在线国偷精品产拍免费yy| 欧美夜福利tv在线| 香蕉成人久久| 精品999日本| 老牛嫩草一区二区三区日本| 久久精品亚洲一区二区| 韩国视频理论视频久久| 久久久爽爽爽美女图片| 久久久久国产精品www| 在线成人www免费观看视频| 欧美xxx在线观看| 欧美成人午夜激情| 一区二区三区色| 亚洲性夜色噜噜噜7777| 国产亚洲福利一区| 免费日韩av电影| 欧美全黄视频| 欧美一区日韩一区| 久久美女艺术照精彩视频福利播放| 国模精品一区二区三区色天香| 久久天堂成人| 欧美伦理视频网站| 篠田优中文在线播放第一区| 久久久久国产一区二区三区| 亚洲人成啪啪网站| 亚洲最新在线| 狠狠色丁香久久综合频道| 亚洲国内高清视频| 国产乱子伦一区二区三区国色天香| 久久全球大尺度高清视频| 久久久久久9| 亚洲专区在线| 欧美aaa级| 久久99在线观看| 蜜桃av一区二区| 欧美一区2区三区4区公司二百| 久久av一区二区三区| 亚洲老司机av| 欧美综合国产| 亚洲女同精品视频| 麻豆精品视频| 欧美中文在线观看| 欧美日韩免费观看一区三区| 久久久久久国产精品mv| 欧美日韩成人在线| 久久蜜臀精品av| 国产精品二区在线| 国产自产2019最新不卡| 午夜激情亚洲| 久久综合久色欧美综合狠狠| 亚洲在线视频观看| 欧美大片在线观看一区二区| 欧美在线一二三| 欧美日韩国产电影| 老鸭窝毛片一区二区三区| 国产精品白丝av嫩草影院| 欧美韩国日本综合| 国产乱码精品一区二区三区五月婷| 亚洲国产成人在线播放| 国产精品欧美日韩一区二区| 亚洲国产精品久久久久秋霞影院 | 国产私拍一区| 日韩一二在线观看| 亚洲大胆女人| 欧美一区二区三区在线看| 亚洲天堂网在线观看| 免费成人av资源网| 免费在线亚洲欧美| 国产日产精品一区二区三区四区的观看方式 | 亚洲第一主播视频| 欧美伊人久久大香线蕉综合69| 亚洲乱亚洲高清| 鲁大师影院一区二区三区| 久久精彩视频| 国产欧美日韩综合精品二区| 亚洲欧美另类综合偷拍| 亚洲一区二区在| 欧美日韩精品国产| 亚洲黄色免费| 亚洲人成亚洲人成在线观看图片 | 亚洲精一区二区三区| 猫咪成人在线观看| 久久青青草综合| 一区二区在线观看视频在线观看| 午夜视频一区在线观看| 午夜亚洲福利在线老司机| 国产精品爱久久久久久久| 日韩亚洲欧美一区| 亚洲一区自拍| 国产精品久久久久永久免费观看 | 久久人人爽人人爽| 国产亚洲福利| 久久久青草婷婷精品综合日韩 | 国产欧美日韩一区二区三区| 一本久道久久综合婷婷鲸鱼| 日韩一级黄色片| 欧美日韩视频不卡| 中文一区二区| 欧美亚洲尤物久久| 国产在线视频欧美| 久久久久一区二区| 亚洲第一精品夜夜躁人人爽 | 久久激情综合| 在线观看日韩精品| 欧美激情中文字幕一区二区| 欧美激情亚洲一区| 日韩午夜中文字幕| 国产精品爱久久久久久久| 午夜日韩电影| 欧美成人精品三级在线观看| 亚洲剧情一区二区| 国产精品久久久久久久久久直播| 亚洲肉体裸体xxxx137| 99在线精品视频在线观看| 欧美日韩专区| 久久天堂精品| 亚洲激情另类| 亚洲欧美日韩视频二区| 黄色成人91| 欧美激情中文字幕在线| 亚洲天堂免费观看| 麻豆精品视频在线| 日韩视频免费观看| 国产三区二区一区久久| 久久精品一本| 亚洲免费激情| 久久久99免费视频| 亚洲伦伦在线| 国产欧美日韩综合| 欧美激情一二三区| 性8sex亚洲区入口| 免费成年人欧美视频| 亚洲一区免费| 日韩视频免费| 狠狠色香婷婷久久亚洲精品| 欧美激情精品久久久久久黑人 | 久久香蕉精品| 欧美在线免费| 中文高清一区| 亚洲三级影院|