Black Horse programmer ___ OC _ ARCHIVE

Source: Internet
Author: User

 

将数据以一定的格式写到文件,等到需要用的时候直接按照原来的形式读出来,这种方式叫做归档

#import <Foundation/Foundation.h>int main(int argc, const char * argv[]) {    @autoreleasepool {                                NSDictionary *dict =[[NSDictionary alloc] initWithObjectsAndKeys:@"one",@"1",@"two",@"2",@"three",@"3", nil];                //将字典写到文件夹里面        [dict writeToFile:@"/Users/fengze/Desktop/test1" atomically:YES];                        //将文件读出来,直接生成一个字典//         NSDictionary dict2 = [[NSDictionary alloc] initWithContentsOfFile:@"/Users/fengze/Desktop/test1"];           NSArray *array = [[NSArray alloc] initWithObjects:@"one",@"two",@"three", nil];                NSMutableData *data = [[NSMutableData alloc] init];                //当字典和数组想同时写入一个文件的时候                //创建一个归档管理员        NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];                //将数据打包        [archiver encodeObject:array forKey:@"array"];        [archiver encodeObject:dict forKey:@"dict"];        //完成打包        [archiver finishEncoding];        [data writeToFile:@"/Users/fengze/Desktop/test1" atomically:YES];                //反归档管理员        NSKeyedUnarchiver *unrchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];                //将打包的数组读出来        NSArray *array2 = [unrchiver decodeObjectForKey:@"array"];        //将打包的字典读出来        NSDictionary *dict2 = [unrchiver decodeObjectForKey:@"dict"];        }    return 0;}

 

黑马程序员___OC__归档

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.