• 文件归档


     

    #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;

    }

     

    让明天,不后悔今天的所作所为
  • 相关阅读:
    学习MongoDB(Troubleshoot Replica Sets) 集群排除故障
    MyBatis 相同事物查询缓存问题
    Spring事物源码
    Spring Session Redis
    Tomcat配置多个域名绑定到不同项目
    Shiro相关文章资料
    一网打尽:Java 程序员必须了解的计算机底层知识!
    Chrome 80 调教篇
    谭浩强《C++程序设计》
    HTTP/HTTPS协议
  • 原文地址:https://www.cnblogs.com/-yun/p/4324327.html
Copyright © 2020-2023  润新知