• <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>

            逛奔的蝸牛

            我不聰明,但我會很努力

               ::  :: 新隨筆 ::  ::  :: 管理 ::
            @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 逛奔的蝸牛 閱讀(546) 評論(0)  編輯 收藏 引用 所屬分類: Cocoa
            99久久久精品免费观看国产| 久久人人爽人人精品视频| 伊人久久精品无码二区麻豆| 99久久国产宗和精品1上映 | 国内精品伊人久久久影院| 久久国产欧美日韩精品免费| 99久久精品国产免看国产一区| 国产精品伊人久久伊人电影| 亚洲国产美女精品久久久久∴| 99久久伊人精品综合观看| 亚洲乱码精品久久久久..| 久久99精品久久久久久秒播| 久久久久99精品成人片欧美| 久久综合伊人77777麻豆| 97久久精品人妻人人搡人人玩| 久久人妻少妇嫩草AV无码蜜桃| 九九久久自然熟的香蕉图片| 国产精品久久新婚兰兰| 久久国产视屏| 国产99久久久国产精品~~牛| 精品国产一区二区三区久久久狼| 欧美成人免费观看久久| 久久午夜福利电影| 久久国产香蕉一区精品| 国产精品九九久久免费视频 | 久久精品女人天堂AV麻| 久久se精品一区精品二区| 久久亚洲美女精品国产精品| 精品久久久久久久国产潘金莲| 青青草原综合久久大伊人导航| 国产精品99久久不卡| 激情综合色综合久久综合| 久久91精品综合国产首页| 精品一久久香蕉国产线看播放| 精品久久久久一区二区三区| 精品久久久久久无码人妻热| 久久天天躁狠狠躁夜夜2020老熟妇| 国产精品成人精品久久久| 久久久久综合中文字幕| 偷偷做久久久久网站| 国产精品99久久久精品无码|