Believe in your own future, will thank yourself right now Sinner Yun

Sinner_Yun

文件归档

 

#import <Foundation/Foundation.h>

 

//plist

//代码方式

//

//plist 文件的根节点只能是数组或者字典

//plist 文件只能存储 NSString NSArray NSDictionary

//NSData NSDate NSNumber BOOL

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

    @autoreleasepool {

//        NSFileManager *fm = [NSFileManager defaultManager];

//        [fm createFileAtPath:@"/Users/zhangxueming/Desktop/test/app.plist" contents:nil attributes:nil];//创建一个plist文件

        //以字典为根节点创建plist文件

        

        NSMutableDictionary *mulDict = [NSMutableDictionary dictionary];

        [mulDict setObject:@"1" forKey:@"one"];

        [mulDict setObject:@"2" forKey:@"two"];

        [mulDict setObject:@"3" forKey:@"three"];

        [mulDict setObject:[NSNumber numberWithInt:123] forKey:@"number"];

        [mulDict setObject:[NSDate date] forKey:@"date"];

        [mulDict writeToFile:@"/Users/zhangxueming/Desktop/test/ios.plist" atomically:YES];//如果该文件不存在,自动创建该文件

        

        //以数组为根节点创建plist文件

        

        NSMutableArray *mulArr = [NSMutableArray array];

        [mulArr addObject:@"qianfeng"];

        [mulArr addObject:@"hello world"];

        [mulArr addObject:mulDict];

        [mulArr addObject:[NSNumber numberWithBool:YES]];

        [mulArr writeToFile:@"/Users/zhangxueming/Desktop/test/array.plist" atomically:YES];

        

        //直接在xcode中创建plist文件

        //xcode --> Resourse --> Property List

    }

    return 0;

}

 

 

 

 

 

#import <Foundation/Foundation.h>

 

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

    @autoreleasepool {

        //如果文件根节点是NSDictionary

        //用字典类的方法读取plist文件

        //- (NSDictionary *)initWithContentsOfFile:(NSString *)path;

        NSDictionary  *dictContent = [NSDictionary dictionaryWithContentsOfFile:@"/Users/zhixiang/Desktop/qfile.plist"];

        

        NSLog(@"dict = %@", dictContent);

        

        

        //NSArray

        //- (NSArray *)initWithContentsOfFile:(NSString *)path;

        NSArray *arrayContent = [NSArray arrayWithContentsOfFile:@"/Users/zhixiang/Desktop/qfile.plist"];

        NSLog(@"arrayContent = %@", arrayContent);

    }

    return 0;

}

 

 

 

 

#import <Foundation/Foundation.h>

 

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

    @autoreleasepool {

        

        char buf[100]={};

        scanf("%s", buf);

        NSString *dstStr = [NSString stringWithUTF8String:buf];

        

        NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:@"/Users/zhangxueming/Desktop/ios1501/ios1501OC讲课/day8_文件归档/OC4_plist文件练习/OC4_plist文件练习/qfile.plist"];

        

        NSDictionary *framesObj = [dict objectForKey:@"frames"];

        

        NSDictionary *pictureObj = [framesObj objectForKey:dstStr];

        

        NSLog(@"%@", [pictureObj objectForKey:@"textureRect"]);

    }

    return 0;

}

 

 

 

 

#import <Foundation/Foundation.h>

//归档: 把内存中的数据按照指定的格式存储到指定的文件中的过程

//解归档: 把文件中存储的数据读取到内存中

 

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

    @autoreleasepool {

#if 0

        //归档

        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"one",@"2",@"two",@"3",@"three", nil];

        BOOL ret = [NSKeyedArchiver archiveRootObject:dict toFile:@"/Users/zhangxueming/Desktop/test/archfile"];

        if (ret) {

            NSLog(@"文件归档成功");

        }

        else

        {

            NSLog(@"文件归档失败");

        }

#else   

      //解归档

        NSDictionary *dict = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/zhangxueming/Desktop/test/archfile"];

        NSLog(@"dict = %@", dict);

#endif

    }

    return 0;

}

 

 

 

 

 

#import <Foundation/Foundation.h>

 

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

    @autoreleasepool {

#if 0

        NSString *str = @"千锋教育";

        NSData *data= [NSKeyedArchiver archivedDataWithRootObject:str];

        BOOL ret = [data writeToFile:@"/Users/zhangxueming/Desktop/test/datafile" atomically:YES];

        if (ret) {

            NSLog(@"文件归档成功");

        }

        else

        {

            NSLog(@"文件归档失败");

        }

#else

        //解归档

        NSData *data = [NSData dataWithContentsOfFile:@"/Users/zhangxueming/Desktop/test/datafile"];

        NSString *strObj=[NSKeyedUnarchiver unarchiveObjectWithData:data];

        NSLog(@"str = %@", strObj);

        

#endif

        

    }

    return 0;

}

 

 

 

 

#import <Foundation/Foundation.h>

 

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

    @autoreleasepool {

#if 0

        NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"1",@"one",@"2",@"two",@"3",@"three", nil];

        NSArray *arr = [[NSArray alloc] initWithObjects:@"hello",@"world",@"qian",@"feng", nil];

    

        NSMutableData *mulData = [NSMutableData data];

        //通过mulData构造archiver

        NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mulData];

        

        //编码

        [archiver encodeObject:dict forKey:@"dictionary"];

        [archiver encodeObject:arr forKey:@"array"];

        //结束编码

        [archiver finishEncoding];

        

        //写文件

        BOOL ret =[mulData writeToFile:@"/Users/zhangxueming/Desktop/test/手动归档.txt" atomically:YES];

        if (ret) {

            NSLog(@"文件归档成功");

        }

        else

        {

            NSLog(@"文件归档失败");

        }

#else

    //解归档

        NSData *data = [NSData dataWithContentsOfFile:@"/Users/zhangxueming/Desktop/test/手动归档.txt"];

        

        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];

        

        NSDictionary *dict = [unarchiver decodeObjectForKey:@"dictionary"];

        NSArray *array = [unarchiver decodeObjectForKey:@"array"];

        

        NSLog(@"dict = %@", dict);

        NSLog(@"array = %@", array);

#endif

    }

    return 0;

}

 

 

 

 

 

#import <Foundation/Foundation.h>

//json javaScript 子集

//"key":"value"

//最外层结构 为字典(居多)或者数组

 

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

    @autoreleasepool {

        NSData *data = [NSData dataWithContentsOfFile:@"/Users/zhangxueming/Desktop/json.txt"];

        

        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

        //NSLog(@"dict = %@", dict);

        NSDictionary *weather = [dict objectForKey:@"weatherinfo"];

        NSLog(@"%@", weather);

        

//        for (id obj in weather) {

//            NSLog(@"%@:%@",obj, [weather objectForKey:obj]);

//        }

        

    }

    return 0;

}

 

 

 

#import <Foundation/Foundation.h>

 

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

    @autoreleasepool {

        NSURL *url = [NSURL URLWithString:@"http://ip.taobao.com/service/getIpInfo.php?ip=63.223.108.42"];

        

        NSData *data = [NSData dataWithContentsOfURL:url];

        

        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

        NSLog(@"dict = %@", dict);

        

    }

    return 0;

}

 

posted on 2014-03-09 19:14  Sinner_Yun  阅读(304)  评论(0编辑  收藏  举报

导航