Saving an NSMutableArray to Core Data

前端 未结 1 1887
野的像风
野的像风 2020-12-08 02:55

I want to add an NSMutableArray of NSStrings to one of my Entities in my core data model. The problem is that this isn\'t a supported type in Core Data.

I tried m

相关标签:
1条回答
  • 2020-12-08 03:36

    You could have a binary data attribute in your modeled object, archive the array to data, and hand it off to the object.

    But I think the better way would be to have a to-many relationship, instead of using an array directly.

    ****Edit: Here's how to archive the array into NSData so that it can be used in your managed object***

    NSData *arrayData = [NSKeyedArchiver archivedDataWithRootObject:[NSArray arrayWithObjects:@"1",@"2", nil]];
    

    Basically, any class you have which conforms to the NSCoding protocol can be archived in this way. NSArray/NSMutableArray already conform to it. They tell all of their objects to archive themselves, so they must conform too. And all of those objects' members must conform, etc. It's like a tree.

    Since your array conforms, and it's an array of NSString (which also conforms), then you're golden.

    0 讨论(0)
提交回复
热议问题