How to store custom objects with struct in Coredata

后端 未结 3 1540
轮回少年
轮回少年 2020-12-10 17:38

I have to store a custom class object to Coredata. The problem is my custom class contains structs,enum etc.I tried following method.

-(void)encodeWit

3条回答
  •  感动是毒
    2020-12-10 18:30

    You can wrap the struct in a NSData, ie

    To encode with archiver

    [coder encodeObject:[NSData dataWithBytes:&my_struct length:sizeof(my_struct)] 
                 forKey:@"my_struct"];
    

    and to decode with archiver

    NSData *data = [coder decodeObjectForKey:@"my_struct"];
    [data getBytes:&my_struct length:sizeof(my_struct)];
    

提交回复
热议问题