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

攀升·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>
            国产精品卡一卡二| 欧美高清视频www夜色资源网| 欧美日韩免费观看一区| 亚洲精品国产精品国自产在线| 噜噜噜躁狠狠躁狠狠精品视频| 久久野战av| 91久久久在线| 99国产精品久久久久久久久久| 国产精品sss| 久久精品国产久精国产爱| 久久久.com| 日韩亚洲国产欧美| 亚洲综合欧美| 在线观看日韩av| 亚洲日韩中文字幕在线播放| 欧美日韩精品不卡| 久久精品一区二区三区不卡牛牛| 久久久欧美精品| 99视频一区二区三区| 亚洲一区二区三区四区五区午夜 | 亚洲在线免费| 国产亚洲精品自拍| 亚洲剧情一区二区| 国产综合色在线| 亚洲精品综合精品自拍| 国产亚洲精品久| 亚洲精品中文字| 韩国一区电影| 亚洲午夜视频| 91久久精品一区二区别| 欧美一区二区三区啪啪| 一区二区欧美国产| 久久精品女人| 欧美一区日韩一区| 欧美精品在线观看播放| 免费成人黄色片| 国产日韩专区| 亚洲作爱视频| 日韩午夜三级在线| 久久亚洲欧洲| 久久在线播放| 国产伦一区二区三区色一情| 亚洲黄色成人久久久| 激情五月***国产精品| 亚洲欧美国产va在线影院| 一本色道久久综合亚洲精品小说| 久久久亚洲国产美女国产盗摄| 亚洲欧美一区二区在线观看| 免费在线欧美黄色| 欧美视频日韩视频| 亚洲二区精品| 国产午夜亚洲精品羞羞网站| 一区二区三区视频在线| 99精品久久免费看蜜臀剧情介绍| 久久久久久久久伊人| 久久国产精品72免费观看| 国产精品日韩欧美一区| 制服丝袜激情欧洲亚洲| 亚洲一区二区精品视频| 欧美日韩高清在线播放| 亚洲精品你懂的| 日韩视频免费看| 欧美另类69精品久久久久9999| 欧美成人激情视频免费观看| 激情综合亚洲| 久久久久一区二区三区| 美女黄毛**国产精品啪啪| 在线不卡亚洲| 美乳少妇欧美精品| 亚洲国产精品999| 日韩网站在线观看| 欧美日韩精品免费观看视频完整| 夜夜嗨av一区二区三区网站四季av| 亚洲免费黄色| 国产精品视频九色porn| 午夜精品久久| 欧美成人r级一区二区三区| 亚洲国产小视频在线观看| 免费中文字幕日韩欧美| 亚洲精品永久免费| 亚洲欧美国产高清| 国产一区二区视频在线观看| 久久精品女人| 亚洲精品国产视频| 午夜性色一区二区三区免费视频| 国产日韩亚洲| 另类综合日韩欧美亚洲| 亚洲日本乱码在线观看| 午夜精品久久久久久久99黑人| 国产亚洲一级| 欧美激情综合亚洲一二区| 亚洲调教视频在线观看| 久久综合导航| 99精品国产一区二区青青牛奶| 国产精品久久久一区二区| 久久精彩免费视频| 亚洲精品裸体| 久久精品三级| 在线一区二区三区四区五区| 国产精品一区二区欧美| 免费亚洲婷婷| 性欧美大战久久久久久久免费观看 | 极品少妇一区二区三区| 欧美成人午夜影院| 性欧美1819sex性高清| 亚洲国产一区二区a毛片| 久久福利资源站| 一本色道精品久久一区二区三区| 国产日韩精品在线观看| 欧美日韩免费一区二区三区视频| 欧美一区二区三区男人的天堂 | 亚洲精品老司机| 午夜一区不卡| 亚洲国产经典视频| 亚洲欧美日韩在线高清直播| 亚洲国产高清自拍| 国产香蕉97碰碰久久人人| 欧美日韩精品二区第二页| 久久夜色精品亚洲噜噜国产mv | 欧美在线播放一区| 夜夜夜精品看看| 亚洲电影一级黄| 国产色产综合产在线视频| 欧美日韩午夜在线| 免费久久99精品国产| 久久成人免费网| 亚洲伊人第一页| 99亚洲视频| 亚洲精品中文在线| 亚洲欧洲精品一区二区| 欧美福利视频网站| 你懂的亚洲视频| 久久亚洲欧洲| 久久久久久电影| 久久久www成人免费精品| 小黄鸭视频精品导航| 亚洲女性裸体视频| 午夜久久黄色| 欧美一级精品大片| 小黄鸭精品aⅴ导航网站入口| 亚洲私人黄色宅男| 亚洲欧美日韩高清| 亚洲欧美国产一区二区三区| 一区二区三区视频在线播放| 一区二区三区四区五区精品视频| 亚洲精品久久久久久下一站| 亚洲精品国产系列| 国产精品99久久久久久久女警 | 激情综合电影网| 精品成人一区二区三区四区| 精品不卡一区| 樱桃国产成人精品视频| 亚洲国产精品欧美一二99| 亚洲激情网址| 一区二区三区成人| 午夜免费电影一区在线观看| 欧美亚洲视频在线观看| 久久成人国产| 欧美69视频| 亚洲毛片网站| 亚洲欧美网站| 老司机免费视频久久| 欧美国产先锋| 国产精品自拍一区| 精品成人一区二区三区| 亚洲精品韩国| 亚洲欧洲99久久| 蜜臀av性久久久久蜜臀aⅴ| 亚洲盗摄视频| 亚洲一区免费| 久久婷婷久久一区二区三区| 欧美精品在线播放| 国产欧美一区二区白浆黑人| 亚洲高清123| 亚洲视频免费在线观看| 久久精品女人| 99re热精品| 久久精品二区| 欧美深夜福利| 尤物在线精品| 亚洲线精品一区二区三区八戒| 久久裸体视频| 99热在这里有精品免费| 久久精品国产欧美亚洲人人爽| 欧美日本网站| 亚洲国产精品ⅴa在线观看 | 91久久久久久国产精品| 欧美一区二区久久久| 国产精品免费一区二区三区在线观看 | 久久伊人精品天天| 欧美香蕉大胸在线视频观看| ●精品国产综合乱码久久久久| 亚洲小视频在线| 欧美电影打屁股sp| 欧美一区三区三区高中清蜜桃| 欧美精品国产精品日韩精品| 国语精品中文字幕| 性欧美大战久久久久久久久| 亚洲精品一区二区三区四区高清| 欧美中日韩免费视频|