Save / Write NSMutableArray of objects to disk?

后端 未结 2 770
温柔的废话
温柔的废话 2021-01-12 16:42

Initially I thought this was going to work, but now I understand it won\'t because artistCollection is an NSMutableArray of \"Artist\" objects.

@interface Ar         


        
2条回答
  •  醉梦人生
    2021-01-12 17:31

    Take a look at NSKeyedArchiver. Briefly :

    NSData* artistData = [NSKeyedArchiver archivedDataWithRootObject:artistCollection];
    [artistData writeToFile: @"/Users/Fgx/Desktop/stuff" atomically:YES];
    

    You'll need to implement encodeWithCoder: on your Artist class - see Apple's docs

    Unarchiving (see NSKeyedUnarchiver) is left as an exercise for the reader :)

提交回复
热议问题