user3335395
user3335395

Reputation: 59

How to store other NSManagedObjects as property of a NSManagedObject

I am new to iOS programming and I'm doing up a simple function of an iOS application. Currently, I've created an entity called Players, and I'd like the entity to have a property, in which it stores other NSManagedObject like an array.

This is because I want a player to be able to have friends in the game and this is the way I've thought of; I could just access a player's friend's list via

[playername friendList]

May I know if this is the way to do so? Because for an entity's attribute type, I couldn't use NSMutableArray or NSArray as its type. If it is, may I know how I can store it? If not, is there a better way to achieve that?

Upvotes: 1

Views: 126

Answers (2)

Aaron Brager
Aaron Brager

Reputation: 66242

You need to create a relationship between the models, which are represented with NSSet (or NSOrderedSet, by checking "ordered", if the order is important.)

Ordered Sets are similar to arrays, except all the objects are distinct (no duplicates).

Upvotes: 1

Wain
Wain

Reputation: 119031

This is the purpose of relationships in the Core Data model. Add a relationship between the two entities (and an inverse) and add the managed objects to that relationship.

See this section of the Core Data guide.

Upvotes: 2

Related Questions