Objective-C 学习笔记9 使用NSKeyedArchiver 归档

还是上一篇的例子

//
//  main.m
//  sample005
//
//  Created by echoliu on 13-1-25.
//  Copyright (c) 2013年 echoliu. All rights reserved.
//

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
         
     
         NSDictionary *glossary=[NSDictionary dictionaryWithObjectsAndKeys:@"a class",@"a",
         @"b class",@"b",
         @"c class",@"c",nil];
        if ([NSKeyedArchiver archiveRootObject:glossary toFile: @"lossay.archiver"]==NO) {
            NSLog(@"save to file failed");
        } 
        
    }
    return 0;
}

//
//  main.m
//  sample005
//
//  Created by echoliu on 13-1-25.
//  Copyright (c) 2013年 echoliu. All rights reserved.
//

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        
        // insert code here...
        //NSLog(@"Hello, World!");
       /*
        NSDictionary *glossary=[NSDictionary dictionaryWithObjectsAndKeys:@"a class",@"a",
                            @"b class",@"b",
                            @"c class",@"c",nil];
        if([glossary writeToFile:@"glossary" atomically:YES]==NO)
        NSLog(@"save to file failed");
        */
        /*
        NSDictionary *glossary ;
        glossary=[NSDictionary dictionaryWithContentsOfFile:@"glossary"];
        for (NSString *str in glossary){
            NSLog(@"%@ %@ ",str,[glossary objectForKey:str]);
        }
        */
     
        /*
         NSDictionary *glossary=[NSDictionary dictionaryWithObjectsAndKeys:@"a class",@"a",
         @"b class",@"b",
         @"c class",@"c",nil];
        if ([NSKeyedArchiver archiveRootObject:glossary toFile: @"lossay.archiver"]==NO) {
            NSLog(@"save to file failed");
        } */
        
         NSDictionary *glossary ;
        glossary=[NSKeyedUnarchiver unarchiveObjectWithFile:@"lossay.archiver"];
         for (NSString *str in glossary){
         NSLog(@"%@ %@ ",str,[glossary objectForKey:str]);
         }
         
    }
    return 0;
}

实例来源教材

posted on 2013-01-26 09:28  ios开发达人  阅读(312)  评论(0编辑  收藏  举报