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

攀升·Uranus


Something Different,Something New
數據加載中……

iOS寫文件補充

一 系統內置對象讀寫至文件

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

 二 自定義對象讀寫至文件

 -   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讀寫文件

- 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 攀升 閱讀(868) 評論(0)  編輯 收藏 引用 所屬分類: 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>
            午夜视频一区二区| 日韩亚洲欧美成人| 久久久久久久久久久一区 | 亚洲欧美中文另类| 国产一区二区黄色| 欧美国产精品v| 欧美激情精品久久久久| 亚洲一区视频在线| 香蕉久久久久久久av网站| 好吊日精品视频| 亚洲高清不卡一区| 欧美视频一区二区三区…| 欧美一区二区黄色| 噜噜噜噜噜久久久久久91| 在线中文字幕一区| 午夜视频一区二区| 99国产精品国产精品毛片| 亚洲一区二区三区欧美| 亚洲第一精品电影| 亚洲特黄一级片| 在线观看三级视频欧美| 一本色道久久综合亚洲精品按摩| 国产日韩精品久久久| 亚洲国产第一页| 国产伦精品一区二区三区| 欧美高清在线一区二区| 国产精品女人久久久久久| 美女视频一区免费观看| 国产精品大片| 亚洲国产精品久久91精品| 国产精品尤物| 日韩视频一区二区三区在线播放| 狠狠久久五月精品中文字幕| 一本一本久久a久久精品综合麻豆| 国产综合一区二区| 亚洲午夜久久久久久久久电影网| 亚洲欧洲精品一区二区三区| 欧美一级二区| 午夜精品久久久久影视 | 欧美成人免费网站| 久久久福利视频| 国产精品三区www17con| 亚洲国产成人tv| 在线日韩精品视频| 久久成人精品电影| 久久精品99无色码中文字幕 | 欧美日韩国产天堂| 亚洲第一页中文字幕| 激情五月***国产精品| 亚洲视频观看| 亚洲制服少妇| 国产精品国产三级国产| 亚洲精品四区| 一区二区三区欧美在线观看| 欧美不卡在线| 亚洲国产成人久久综合一区| 一区国产精品| 久久免费少妇高潮久久精品99| 久久精品免费播放| 国产一区二区主播在线| 亚洲欧美日韩久久精品| 欧美一区二区大片| 国产视频在线一区二区| 午夜精品久久一牛影视| 久久精品99久久香蕉国产色戒| 国产日韩欧美亚洲| 欧美一区高清| 美女精品在线| 亚洲激情视频在线播放| 欧美国产日韩免费| 亚洲美女尤物影院| 亚洲免费一在线| 国产精品视频专区| 午夜精品久久久久久久男人的天堂| 性刺激综合网| 国产欧美日韩免费| 久久国产精品99久久久久久老狼| 久久一区二区三区四区五区| 在线成人激情视频| 欧美国产极速在线| 99国产精品99久久久久久粉嫩| 亚洲自拍偷拍色片视频| 国产精品一区二区久久久| 欧美一级视频免费在线观看| 欧美aaa级| 亚洲一区二区在线看| 国产欧美日韩综合一区在线观看 | 小处雏高清一区二区三区| 久久免费视频在线观看| 亚洲激情另类| 国产精品r级在线| 小处雏高清一区二区三区| 免费一级欧美在线大片| 一本综合久久| 国产亚洲精品aa午夜观看| 蜜臀91精品一区二区三区| 最新亚洲电影| 欧美一级欧美一级在线播放| 在线观看福利一区| 欧美先锋影音| 蜜臀久久99精品久久久久久9 | 国产一区二区精品久久| 欧美aaa级| 午夜视频在线观看一区二区三区| 欧美成在线观看| 欧美一区二视频在线免费观看| 亚洲国产天堂久久综合| 国产精品美腿一区在线看| 久久夜色精品国产欧美乱| 亚洲一级一区| 亚洲国产日韩欧美在线动漫| 久久精品国产一区二区电影| 一本色道久久加勒比88综合| 伊人色综合久久天天五月婷| 欧美日韩精品一区二区| 久久综合色88| 欧美在线www| 亚洲一二三区在线| 亚洲精品乱码久久久久| 免费欧美在线| 久久亚洲精品中文字幕冲田杏梨| 亚洲欧美久久久久一区二区三区| 亚洲久久成人| 亚洲日本电影在线| 亚洲黄色在线观看| 在线成人欧美| 伊人夜夜躁av伊人久久| 狠狠色香婷婷久久亚洲精品| 国产精品一区二区三区久久| 欧美丝袜一区二区三区| 欧美精品免费在线| 欧美激情亚洲综合一区| 麻豆亚洲精品| 免费日韩一区二区| 美女999久久久精品视频| 久久久久久9999| 欧美中文字幕精品| 欧美在线一区二区三区| 欧美自拍丝袜亚洲| 欧美主播一区二区三区美女 久久精品人| 亚洲视频一区二区在线观看| 一本色道久久综合一区| 亚洲精品永久免费精品| 亚洲人成7777| 亚洲国产精品v| 亚洲黄色在线| 亚洲美女精品久久| 一区二区日本视频| 亚洲一区二区在线看| 亚洲女同在线| 欧美一站二站| 老司机午夜精品视频在线观看| 欧美.com| 欧美日韩在线观看视频| 国产精品视频一区二区高潮| 国产午夜精品全部视频播放| 精品动漫一区二区| 亚洲国产天堂久久国产91| 亚洲精品一区二区三区av| 亚洲视频一起| 欧美在线看片| 欧美1区免费| 亚洲精品偷拍| 欧美一区二区女人| 免费久久99精品国产自在现线| 欧美精品免费看| 国产区精品视频| 亚洲国产视频一区| 亚洲欧美日韩综合国产aⅴ| 久久精品99国产精品酒店日本| 欧美高清成人| 亚洲无线视频| 欧美va日韩va| 国产欧美日韩综合| 亚洲激情在线| 欧美在线啊v| 亚洲日本激情| 久久精品国产亚洲一区二区| 欧美日韩国产a| 国模吧视频一区| 在线视频亚洲一区| 另类图片综合电影| 在线综合亚洲欧美在线视频| 久久久亚洲影院你懂的| 欧美少妇一区二区| 亚洲国产日韩欧美在线99| 午夜精品久久| 91久久综合亚洲鲁鲁五月天| 欧美一级电影久久| 国产精品家庭影院| 亚洲日韩中文字幕在线播放| 久久国产综合精品| 一区二区三区久久精品| 免费久久99精品国产| 国产一区二区在线观看免费| 亚洲视频一二三| 亚洲精品免费观看| 欧美成人国产va精品日本一级| 国内不卡一区二区三区| 小黄鸭精品aⅴ导航网站入口|