Objective-C 学习笔记8 归档

对于NSString NSDictionary NSArray NSData NSNumber 使用自身带的writeToFile 等方法即可

如下  测试环境 归档

//
//  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");
        
    }
    return 0;
}

从归档中初始化值,使用  

dictionaryWithContentsOfFile

如果是NSArray就是用 arrayWithContentsOfFile 其他类似

//
//  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...

        NSDictionary *glossary ;
        glossary=[NSDictionary dictionaryWithContentsOfFile:@"glossary"];
        for (NSString *str in glossary){
            NSLog(@"%@ %@ ",str,[glossary objectForKey:str]);
        }
        
    }
    return 0;
}

 好了,这些都是简单的一个方法调用,那么关于类,和复杂类怎么操作呢,继续学习...

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