NSManagedObject as NSDictionary key?

前端 未结 4 1710
孤街浪徒
孤街浪徒 2021-01-01 15:30

In my app, I have a NSDictionary whose keys should be instances of a subclass of NSManagedObject.

The problem, however, is that NSMa

4条回答
  •  温柔的废话
    2021-01-01 15:59

    I had a similar problem, in which I needed to bundle several entities with additional data for each, and initially tried:

    @{entity1:data1, @entity2:data2, @entity3:data3}
    

    this didn't work for the reason above (NSCopying), so I did:

    @[
       @{@"entity":entity1, @"data":data1},
       @{@"entity":entity2, @"data":data2},
       @{@"entity":entity3, @"data":data3}
    ]
    

    But this solution makes sense only if you don't need dictionary style access to these entities or are happy to iterate to find what you need. In my case this was a packaging problem. Note that if you pass these entities around the NSManagedObjectContext need to be the same to use them.

提交回复
热议问题