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

逛奔的蝸牛

我不聰明,但我會很努力

   ::  :: 新隨筆 ::  ::  :: 管理 ::
@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>
            欧美三区在线视频| 亚洲欧美激情一区| 性久久久久久久久久久久| 亚洲美女毛片| 在线视频欧美日韩| 亚洲一区久久| 久久久99精品免费观看不卡| 久久精品国产免费看久久精品| 久久伊伊香蕉| 国产精品jizz在线观看美国| 中文一区在线| 亚洲欧美色婷婷| 性欧美大战久久久久久久免费观看 | 国语自产精品视频在线看8查询8 | 亚洲三级视频| 91久久久久久国产精品| 亚洲免费av片| 欧美一区二区三区在线播放| 久久久夜夜夜| 欧美性事免费在线观看| 好吊色欧美一区二区三区四区| 亚洲美女在线国产| 久久国产欧美| 亚洲毛片av在线| 久久精品一区| 国产精品福利影院| 亚洲国产色一区| 久久成人综合视频| 亚洲国产成人精品久久久国产成人一区 | 欧美国产日韩在线| 国产欧美日韩三区| 亚洲伦理久久| 久久影音先锋| 午夜亚洲精品| 国产精品啊v在线| 亚洲日本va午夜在线电影| 久久九九99视频| 99精品欧美| 欧美本精品男人aⅴ天堂| 国产一区二区精品久久91| 一道本一区二区| 亚洲第一区在线观看| 欧美一区二区三区免费看| 欧美亚洲成人免费| 亚洲最新在线| 亚洲人久久久| 欧美精品久久久久久| 91久久精品一区二区别| 免费在线观看成人av| 久久xxxx精品视频| 国产日韩欧美不卡| 久久国产色av| 欧美在线在线| 狠狠狠色丁香婷婷综合激情| 久久精品毛片| 欧美中日韩免费视频| 校园激情久久| 一区二区日韩| 欧美日韩在线播放一区二区| 亚洲精一区二区三区| 欧美黄色影院| 欧美日本一区二区三区| 日韩视频专区| 亚洲美女在线国产| 欧美性事免费在线观看| 午夜日韩福利| 久久av红桃一区二区小说| 国产午夜精品久久久久久久| 欧美伊久线香蕉线新在线| 亚洲欧美在线aaa| 激情av一区二区| 亚洲高清免费在线| 欧美国产一区在线| 亚洲一区二区三区激情| 亚洲欧美日韩一区二区| 一区二区三区自拍| 91久久久精品| 国产精品一卡二| 六月婷婷久久| 欧美人妖在线观看| 欧美一区二区三区在线| 久久精品免费| 一本色道久久99精品综合 | 久久男人资源视频| 亚洲精品欧美一区二区三区| 亚洲毛片一区| 国产一区二区三区成人欧美日韩在线观看 | 亚洲精品欧美极品| 亚洲视频久久| 在线看国产一区| 日韩亚洲欧美成人| 国产亚洲精品久久久久久| 亚洲成色www8888| 国产精品久久999| 免费短视频成人日韩| 欧美日韩综合视频网址| 久久免费视频网站| 欧美色视频在线| 欧美www视频| 国产精品久久久久91| 免费成人黄色片| 国产精品麻豆成人av电影艾秋| 久久综合九色欧美综合狠狠| 欧美日韩一级黄| 欧美不卡福利| 国产视频亚洲精品| 一本色道久久综合亚洲精品不| 伊人久久噜噜噜躁狠狠躁| 在线视频欧美一区| 日韩视频国产视频| 久久久不卡网国产精品一区| 亚洲一级免费视频| 欧美激情一区二区三区不卡| 久久亚洲二区| 国产日韩欧美一区二区三区在线观看| 亚洲欧美成人网| 亚洲日本无吗高清不卡| 国产精品视频一二三| 亚洲国产精品久久久久秋霞蜜臀 | 亚洲韩国青草视频| 樱花yy私人影院亚洲| 亚洲一区二区三区在线| 一本色道久久精品| 欧美电影免费观看| 欧美成人精品不卡视频在线观看| 国产精品自拍小视频| 一区二区精品在线| 亚洲视频久久| 欧美三级欧美一级| 日韩亚洲在线| 亚洲性感美女99在线| 欧美日韩三区| 一本色道久久88综合亚洲精品ⅰ| 亚洲免费av观看| 欧美黄色小视频| 亚洲国产视频一区二区| 亚洲精品人人| 欧美久久精品午夜青青大伊人| 亚洲二区视频| 日韩视频亚洲视频| 欧美精品在线网站| 亚洲精品免费看| 亚洲先锋成人| 国产免费观看久久| 欧美在线观看天堂一区二区三区 | 亚洲永久精品大片| 午夜国产欧美理论在线播放 | 国产精品丝袜91| 午夜精品成人在线| 另类av一区二区| 亚洲精品在线观| 欧美日韩国产在线观看| 一区二区三区www| 久久福利视频导航| 亚洲第一天堂无码专区| 欧美高清视频| 亚洲免费影视| 免费成人网www| 一本色道久久综合狠狠躁篇的优点| 欧美日韩精品免费看 | 国产精品第一区| 欧美专区在线播放| 亚洲国产99精品国自产| 亚洲午夜一级| 精品51国产黑色丝袜高跟鞋| 欧美大片免费观看| 亚洲欧美久久久| 欧美高清不卡| 欧美一区二区成人6969| 亚洲国产中文字幕在线观看| 欧美色网一区二区| 欧美综合第一页| 日韩视频一区二区三区在线播放| 欧美在线一级va免费观看| 亚洲电影在线看| 国产精品免费网站在线观看| 欧美亚洲一区在线| 欧美日韩亚洲91| 欧美一站二站| 亚洲免费av电影| 麻豆成人小视频| 亚洲一区二区动漫| 亚洲高清视频在线观看| 国产精品人人做人人爽人人添| 久久久国产成人精品| 这里只有精品视频| 欧美激情第3页| 久久裸体视频| 午夜久久99| 一本一道久久综合狠狠老精东影业 | 亚洲欧美制服另类日韩| 亚洲人成高清| 欧美成人情趣视频| 久久精品卡一| 亚洲欧美中文另类| 亚洲手机成人高清视频| 亚洲精品精选| 亚洲欧洲精品一区二区三区 | 中文无字幕一区二区三区| 亚洲福利视频一区二区|