zoukankan      html  css  js  c++  java
  • Read and Write NSArray, NSDictionary and NSSet to a File

    查询地址:http://iosdevelopertips.com/data-file-management/read-and-write-nsarray-nsdictionary-and-nsset-to-a-file.html

    With just a few lines of code, you can read/write collections to/from files. The code below shows examples for writing and reading both NSArray and NSDictionary objects, the same logic would apply to an NSSet (or other collection type).

    The example below starts by populating both an array and dictionary, each using the Objective-C literal syntax. Read more about using NSArray literals andNSDictionary literals.

    Read and Write Collections to File

    The process is straightforward:

    – Create the NSArray and NSDictionary objects
    – Obtain the path to write/read the files (application Documents directory)
    – Append filenames to path for each collection type
    – Use the ‘writeToFile:’ method of each object to save contents to file

    NSString  *arrayPath;
    NSString  *dictPath;
    NSArray *array = @[@"IPA", @"Pilsner", @"Stout"];
     
    NSDictionary *dictionary = @{@"key1" : array,
                                 @"key2" : @"Hops",
                                 @"key3" : @"Malt",
                                 @"key4" : @"Yeast" };
     
    // Get path to documents directory
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
      NSUserDomainMask, YES);
     
    if ([paths count] > 0)
    {
      // Path to save array data
      arrayPath = [[paths objectAtIndex:0] 
          stringByAppendingPathComponent:@"array.out"];
     
      // Path to save dictionary
      dictPath = [[paths objectAtIndex:0] 
          stringByAppendingPathComponent:@"dict.out"];
     
      // Write array
      [array writeToFile:arrayPath atomically:YES];
     
      // Write dictionary
      [dictionary writeToFile:dictPath atomically:YES];
    }

    Read NSArray NSDictionary NSSet From File

    The code to read back from the collections is equally as simple, using the methods arrayWithContentsOfFile: and dictionaryWithContentsOfFile: read from the file paths created previously to read the save data:

    // Read both back into new NSArray and NSDictionary object
    NSArray *arrayFromFile = [NSArray arrayWithContentsOfFile:arrayPath];
    NSDictionary *dictFromFile = [NSDictionary dictionaryWithContentsOfFile:dictPath];
     
    // Print the contents
    for (NSString *element in arrayFromFile) 
      NSLog(@"Beer: %@", element);
     
    for (NSString *key in dictFromFile) 
      NSLog(@"%@ : %@", key, [dictionary valueForKey:key]);

    The output in the console window is below:

    Beer: IPA
    Beer: Pilsner
    Beer: Stout
    key1 : (
        IPA,
        Pilsner,
        Stout
    )
    key2 : Hops
    key3 : Malt
    key4 : Yeast
    如果一件事情你觉得难的完不成,你可以把它分为若干步,并不断寻找合适的方法。最后你发现你会是个超人。不要给自己找麻烦,但遇到麻烦绝不怕,更不要退缩。 电工查找电路不通点的最快方法是:分段诊断排除,快速定位。你有什么启示吗? 求知若饥,虚心若愚。 当你对一个事情掌控不足的时候,你需要做的就是“梳理”,并制定相应的规章制度,并使资源各司其职。
  • 相关阅读:
    尚硅谷前端2020Web前端学习记录
    阿里网盘阿里云网盘内测资格获取,阿里网盘开通
    冰眼冷链物流监控平台-2020微服务项目实战
    探花交友智能推荐社交项目-2020Java大数据实战
    互联网Java工程师面试突击三季
    恋上数据结构与算法(一、二、三季)
    布客·ApacheCN 编程/后端/大数据/人工智能学习资源 2020.9
    NumPy 基础知识·翻译完成
    NumPy 初学者指南中文第三版·翻译完成
    NumPy 秘籍中文第二版·翻译完成
  • 原文地址:https://www.cnblogs.com/wvqusrtg/p/4548239.html
Copyright © 2011-2022 走看看