Saving a NSArray

前端 未结 3 1964
忘掉有多难
忘掉有多难 2020-12-02 17:23

I would like to save an NSArray either as a file or possibly use user defaults. Here\'s what I am hoping to do.

  1. Retrieve already saved NSArray (if any).
  2. <
3条回答
  •  自闭症患者
    2020-12-02 18:19

    You could implement NSCoding on the objects the array contains and use NSKeyedArchiver to serialize/deserialize your array to disk.

    BOOL result = [NSKeyedArchiver archiveRootObject:myArray toFile:path];
    

    The archiver will defer to your NSCoding implementation to get serializable values from each object and write a file that can be read with NSKeyedUnarchiver:

    id myArray = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    

    More info in the serialization guide.

提交回复
热议问题