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

攀升·Uranus


Something Different,Something New
數(shù)據(jù)加載中……

iOS寫(xiě)文件補(bǔ)充

一 系統(tǒng)內(nèi)置對(duì)象讀寫(xiě)至文件

- If your objects are of type NSString,NSDictionary,NSArray,NSDate,NSData, or NSNumber, you can use the writeToFile:atomically: method implemented in these classes to write your data to a file. In the case of writing out a dictionary or an array, this method writes the data to the file in the format of an XML property list. Program 19.1 shows how the dictionary you created as a simple glossary in Chapter 15,“Numbers, Strings, and Collections,” can be written to a file as a property list.

 Program 19.1

#import <Foundation/NSObject.h>

#import <Foundation/NSString.h>

#import <Foundation/NSDictionary.h>

#import <Foundation/NSAutoreleasePool.h>

int main (int argc, char *argv[])

int main (int argc, char *argv[])

   {

                NSAutoreleasePool            * pool = [[NSAutoreleasePool alloc] init];

                NSDictionary       *glossary =

                [NSDictionary        dictionaryWithObjectsAndKeys:

                    @”A class defined so other classes can inherit from it.”, @”abstract class”,

                    @”To implement all the methods defined in a protocol”, @”adopt”,

                    @”Storing an object for later use. “,  @”archiving”,

                    nil

                ];

                if ([glossary writeToFile: @”glossary”                  atomically: YES] == NO)

                    NSLog (@”Save to file failed!”);

                [pool drain];

                return 0;

           }


To read an XML property list from a file into your program, you use the dictionaryWithContentsOfFile: or arrayWithContentsOfFile: methods.To read back data, use the dataWithContentsOfFile: method; to read back string objects, use the stringWithContentsOfFile:method. Program 19.2 reads back the glossary written in Program 19.1 and then displays its contents.  


#import <Foundation/NSObject.h> 

#import <Foundation/NSString.h> 

#import <Foundation/NSDictionary.h> 

#import <Foundation/NSEnumerator.h> 

#import <Foundation/NSAutoreleasePool.h> 


int main (int argc, char *argv[]) 

    NSAutoreleasePool  * pool = [[NSAutoreleasePool alloc] init]; 

    NSDictionary *glossary; 


    glossary = [NSDictionary dictionaryWithContentsOfFile: @”glossary”]; 


    for ( NSString *key in glossary ) 

        NSLog (@”%@: %@”,          key, [glossary objectForKey: key]); 


     [pool drain]; 

    return 0; 

 二 自定義對(duì)象讀寫(xiě)至文件

 -   A more flexible approach enables you to save any type of objects to a file, not just strings, arrays, and dictionaries.This is done by creating a keyed archive using the NSKeyedArchiver class.
This implies that you can’t directly archive your AddressBook using this technique because the Objective-C system doesn’t know how to archive an AddressBook object. If you tried to archive it by inserting a line such as NSKeyedArchiver archiveRootObject: myAddressBook toFile: @”addrbook.arch”]; into your program, you’d get the following message displayed if you ran the program:

*** -[AddressBook encodeWithCoder:]: selector not recognized
*** Uncaught exception: <NSInvalidArgumentException>
*** -[AddressBook encodeWithCoder:]: selector not recognized
archiveTest: received signal: Trace/BPT trap
To archive objects other than those listed, you must tell the system how to archive, or encode, your objects, and also how to unarchive, or decode, them.This is done by adding encodeWithCoder: and initWithCoder: methods to your class definitions, according to the <NSCoding> protocol. For our address book example, you’d have to add these methods to both the AddressBook and AddressCard classes.

The encodeWithCoder: method is invoked each time the archiver wants to encode an object from the specified class, and the method tells it how to do so. In a similar manner, the initWithCoder: method is invoked each time an object from the specified class is to be decoded.

Program 19.5 Addresscard.h Interface File
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
#import <Foundation/NSKeyedArchiver.h>
@interface AddressCard: NSObject <NSCoding, NSCopying>
{
NSString  *name;
NSString  *email;
}
@property (copy, nonatomic) NSString *name, *email;
-(void) setName: (NSString *) theName andEmail: (NSString *) theEmail;
-(NSComparisonResult) compareNames: (id) element;
-(void) print;
// Additional methods for NSCopying protocol
-(AddressCard *) copyWithZone: (NSZone *) zone;
-(void) retainName: (NSString *) theName andEmail: (NSString *) theEmail;

@end

-(void) encodeWithCoder: (NSCoder *) encoder
{
[encoder encodeObject: name forKey: @”AddressCardName”];
[encoder encodeObject: email forKey: @”AddressCardEmail”];
}
-(id) initWithCoder: (NSCoder *) decoder
{
name = [[decoder decodeObjectforKey: @”AddressCardName”] retain];
email = [[decoder decodeObjectforKey: @”AddressCardEmail”] retain];
return self;
}

#import “AddressBook.h”
#import <Foundation/NSAu orelea ePool.h>
int main (int argc, char *argv[])
{
 NSString  *aName = @”Julia Kochan”;
NSString  *aEmail = @”jewls337@axlc.com”;
NSString  *bName = @”Tony Iannino”;
NSString  *bEmail = @”tony.iannino@techfitness.com”;
NSString  *cName = @”Stephen Kochan”;
NSString  *cEmail = @”steve@steve_kochan.com”;
NSString  *dName = @”Jamie Baker”;
NSString  *dEmail = @”jbaker@hitmail.com”;
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
AddressCard *card1 = [[AddressCard alloc] init];
AddressCard *card2 = [[AddressCard alloc] init];
AddressCard *card3 = [[AddressCard alloc] init];
AddressCard *card4 = [[AddressCard alloc] init];
AddressBook  *myBook = [AddressBook alloc];
// First set up four address cards
[card1 setName: aName andEmail: aEmail];
[card2 setName: bName andEmail: bEmail];
[card3 setName: cName andEmail: cEmail];
[card4 setName: dName andEmail: dEmail];
myBook = [myBook initWithName: @”Steve’s Address Book”];
// Add some cards to the address book
[myBook addCard: card1];
[myBook addCard: card2];
[myBook addCard: card3];
[myBook addCard: card4];
[myBook sort];
if ([NSKeyedArchiver archiveRootObject: myBook toFile: @”addrbook.arch”] == NO)
NSLog (@”archiving failed”);
[card1 release];
[card2 release];
[card3 release];
[card4 release];
[myBook release];
[pool drain];
return 0;
}

Program 19.7 shows how you can read the archive into memory to set up the address
book from a file.
Program 19.7
#import “AddressBook.h”
#import <Foundation/NSAutoreleasePool.h>
int main (int argc, char *argv[])
{
AddressBook         *myBook;
NSAutoreleasePool   * pool = [[NSAutoreleasePool alloc] init];
myBook = [NSKeyedUnarchiver unarchiveObjectWithFile: @”addrbook.arch”];
[myBook list];
[pool drain];
return 0;
}

三 使用NSData讀寫(xiě)文件

- You might not want to write your object directly to a file using the archiveRootObject:ToFile: method, as was done in the previous program examples.For example, perhaps you want to collect some or all of your objects and store them in a single archive file.You can do this in Objective-C using the general data stream object class called NSData.

int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Foo               *myFoo1 = [[Foo alloc] init];
Foo               *myFoo2;
NSMutableData     *dataArea;
NSKeyedArchiver   *archiver;
AddressBook       *myBook;
// Insert code from Program 19.7 to create an Address Book
// in myBook containing four address cards
[myFoo1 setStrVal: @”This is the string”];
[myFoo1 setIntVal: 12345];
[myFoo1 setFloatVal: 98.6];
// Set up a data area and connect it to an NSKeyedArchiver object
dataArea = [NSMutableData data];
archiver = [[NSKeyedArchiver alloc]
initForWritingWithMutableData: dataArea];
// Now we can begin to archive objects
[archiver encodeObject: myBook forKey: @”myaddrbook”];
[archiver encodeObject: myFoo1 forKey: @”myfoo1”];
[archiver finishEncoding];
// Write the archived data are to a file
if ( [dataArea writeToFile: @”myArchive” atomically: YES] == NO)
NSLog (@”Archiving failed!”);
[archiver release];
[myFoo1 release];
[pool drain];
return 0;
}

- Restoring the data from your archive file is simple:You just do things in reverse. First, you need to allocate a data area like before.Next, you need to read your archive file into the data area; then you have to create an SKeyedUnarchiver object and tell it to decode data from the specified area.You must invoke decode methods to extract and decode your archived objects.When you’re finished, you send a finishDecoding message to the NSKeyedUnarchiver object.

#import <Foundation/NSObject.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSString.h>
#import <Foundation/NSKeyedArchiver.h>
#import <Foundation/NSCoder.h>
#import <Foundation/NSData.h>
#import “AddressBook.h”
#import “Foo.h”
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSData            *dataArea;
NSKeyedUnarchiver *unarchiver;
Foo               *myFoo1;
AddressBook       *myBook;
// Read in the archive and connect an
// NSKeyedUnarchiver object to it
dataArea = [NSData dataWithContentsOfFile: @”myArchive”];
if (! dataArea) {
NSLog (@“Can’t read back archive file!”);
Return (1);
}
unarchiver = [[NSKeyedUnarchiver alloc]
initForReadingWithData: dataArea];
// Decode the objects we previously stored in the archive
myBook = [unarchiver decodeObjectForKey: @”myaddrbook”];
myFoo1 = [unarchiver decodeObjectForKey: @”myfoo1”];
[unarchiver finishDecoding];
[unarchiver release];
// Verify that the restore was successful
[myBook list];
NSLog (“%@\n%i\n%g”, [myFoo1 strVal],
[myFoo1 intVal], [myFoo1 floatVal]);
[pool release];
return 0;
}

參考:Programming.in.Objective-C.2.0.2nd(Addison.Wesley.2009)

posted on 2011-03-09 17:51 攀升 閱讀(875) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): iOS

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            女同性一区二区三区人了人一| 国产精品永久在线| 国产亚洲一区二区精品| 老司机精品视频一区二区三区| 免费观看国产成人| 亚洲尤物在线视频观看| 欧美影院精品一区| 夜夜嗨av一区二区三区四季av | 免费日韩av片| 午夜在线电影亚洲一区| 久久综合网色—综合色88| 亚洲午夜精品17c| 久久久国产精品亚洲一区 | 久久一二三国产| 一区二区三区视频观看| 欧美中文在线观看| 亚洲一区二区3| 蜜臀久久99精品久久久久久9| 亚洲欧美视频在线观看视频| 欧美/亚洲一区| 久久久www成人免费无遮挡大片| 欧美黄色影院| 久热精品视频在线免费观看| 国产精品99一区| 亚洲国产精品专区久久| 国产精品少妇自拍| 亚洲日本欧美天堂| 亚洲国产视频a| 久久国产主播精品| 91久久线看在观草草青青| 久久男女视频| 一区二区三区在线看| 老**午夜毛片一区二区三区| 国产精品第一页第二页第三页| 欧美国产一区二区在线观看| 国产日韩欧美综合| 一个色综合导航| 亚洲精品小视频| 久久在精品线影院精品国产| 久久久国产一区二区三区| 国产精品日韩电影| 一个色综合av| 亚洲视频中文字幕| 欧美女同在线视频| 91久久久亚洲精品| 亚洲精品日韩激情在线电影 | 在线看日韩欧美| 欧美专区亚洲专区| 久久国产免费| 国产午夜精品在线观看| 新67194成人永久网站| 欧美在线日韩| 国产一区二区0| 久久精品国产第一区二区三区| 久久久久久黄| 极品尤物av久久免费看| 久久女同精品一区二区| 免费欧美在线| 亚洲三级国产| 欧美日韩国产成人精品| 一区二区三区 在线观看视频| 亚洲一区二区成人| 国产精品自拍小视频| 欧美一区二区私人影院日本| 久久尤物电影视频在线观看| 1204国产成人精品视频| 欧美黑人国产人伦爽爽爽| 日韩一级在线观看| 性久久久久久久久| 狠狠色狠色综合曰曰| 欧美69wwwcom| 在线视频欧美日韩| 久久精品国产综合精品| 亚洲大片精品永久免费| 欧美成人在线免费观看| 99re6这里只有精品| 久久gogo国模裸体人体| 亚洲福利视频三区| 国产精品jizz在线观看美国| 久久精品夜夜夜夜久久| 亚洲精品视频在线播放| 亚洲欧美综合国产精品一区| 黑人巨大精品欧美黑白配亚洲| 免费成人激情视频| 在线亚洲一区| 美国十次成人| 亚洲一区二区av电影| 激情五月综合色婷婷一区二区| 欧美成熟视频| 欧美一区亚洲一区| 亚洲激情一区二区| 久久er99精品| 亚洲免费av电影| 国产一区二区中文字幕免费看| 欧美成人精品在线播放| 午夜一区二区三区在线观看| 亚洲国产一区二区三区在线播| 香蕉久久夜色| 99re6这里只有精品| 含羞草久久爱69一区| 欧美性一区二区| 欧美α欧美αv大片| 亚洲宅男天堂在线观看无病毒| 欧美激情久久久| 欧美在线视频免费播放| 99国内精品久久| 亚洲大胆女人| 国产午夜精品全部视频播放 | 免费成人高清在线视频| 亚洲综合视频网| 亚洲精品国产精品国自产观看| 国产欧美日韩亚洲精品| 欧美日韩国产综合视频在线观看| 久久精品二区三区| 亚洲一区精品视频| 艳女tv在线观看国产一区| 欧美二区在线观看| 久久婷婷久久| 欧美伊人精品成人久久综合97| 亚洲午夜成aⅴ人片| 亚洲伦理在线| 亚洲精品综合精品自拍| 在线免费不卡视频| 国语自产偷拍精品视频偷| 国产精品永久免费视频| 国产精品白丝av嫩草影院| 欧美日本簧片| 欧美顶级大胆免费视频| 狂野欧美激情性xxxx| 久久久精品日韩| 久久国产精品久久精品国产| 欧美亚洲在线视频| 午夜一区二区三区不卡视频| 亚洲在线成人精品| 亚洲制服少妇| 亚洲欧美视频一区二区三区| 亚洲一区二区免费| 亚洲一区在线观看视频| 亚洲一区视频在线观看视频| 亚洲一区二区三区中文字幕| 一区二区三区视频在线| 中文日韩在线视频| 在线视频欧美一区| 亚洲免费在线精品一区| 性欧美暴力猛交69hd| 欧美一区二区三区四区在线观看地址 | 免费观看30秒视频久久| 久久综合九色欧美综合狠狠| 久久久久欧美精品| 久久久在线视频| 免费精品99久久国产综合精品| 免费在线观看精品| 欧美久久久久久久久久| 欧美日韩国产精品自在自线| 国产精品激情av在线播放| 国产精品一页| 狠狠色综合播放一区二区| 亚洲二区免费| 日韩视频在线观看免费| 亚洲婷婷综合色高清在线| 欧美亚洲在线| 欧美成人免费一级人片100| 最新日韩在线视频| 亚洲视频在线观看一区| 久久精品国产精品| 欧美成人午夜| 国产精品毛片在线| 精品动漫3d一区二区三区| 亚洲精品免费一区二区三区| 亚洲一区三区在线观看| 久久久国产视频91| 亚洲国产中文字幕在线观看| 一本一道久久综合狠狠老精东影业| 亚洲自拍偷拍一区| 老司机精品视频网站| 国产精品igao视频网网址不卡日韩| 国产视频一区二区在线观看| 亚洲国产日韩综合一区| 亚洲综合大片69999| 免播放器亚洲一区| 一区二区三区蜜桃网| 久久免费视频在线| 国产精品久久午夜夜伦鲁鲁| 一区二区视频免费完整版观看| 一本大道久久a久久综合婷婷| 久久激情一区| 亚洲精品美女在线观看| 欧美在线视频免费观看| 欧美日韩国产一级| 韩国一区二区三区美女美女秀| 一区二区三区欧美亚洲| 欧美a级理论片| 亚洲欧美日本视频在线观看| 欧美高清视频在线播放| 国内伊人久久久久久网站视频| 亚洲午夜精品久久| 欧美11—12娇小xxxx| 香蕉成人伊视频在线观看| 欧美人与禽猛交乱配视频| 亚洲第一天堂无码专区|