开发者

Core data vs plist: Making weapons and attachments

So I'm trying to make one-to-many and many-to-many relationships with weapons and attachments, actually much more but keeping it simple here. I can make a one-to-many weapon entity that goes to specific weapons like m16 and ak47. Then I can make a one-to-many attachment entity that has specific entities like suppressor and red dot sight. Then I can add relationships like an inverse relationship from m16 to red dot.

So I believe I can make this diagram using core data's editor, but I'm not sure I could actually use this. If I wanted the user to be able to pick an m16 and then choose appropriate attachments, how would I read this from core data? I don't think I can because I'm not actually making the m16 object with all the possible attachments. I really hope this makes sense.

I started making a plist of all of this information but that sucks because now I have a lo开发者_开发百科t many-to-many relationships that I need to make by hand.

I'm just looking for some sort of direction on the way to go so I don't spend a bunch of time going the least efficient way.


If you want to have relationships between your model objects, stay away from plists.

For your particular case, you want to create two entities with their respective attributes: Weapon and Attachment. The relationships are illustrated below:

Weapon <<-->> Attachment

Attachment <<-->> Weapon

So conceptually you have weapons that can have many attachments, and attachments that can be part of many weapons.

With that in mind, you populate your database with the Weapon instances of your choice, and can do the same with Attachment.

Once that's done, you simply assign an attachment to a weapon through the relationship (or attach a weapon to an attachment through the reverse relationship).

And finally to answer your question, if you want a user to pick a certain Weapon instance and then choose its attachments, provided you already have the attachment instances available for choosing, this is as simple as grabbing a reference to the weapon instance, fetching all the attachments and presenting them as options to the user.

The ones that get picket get added to the relationship and you are ready to go.

Check the Recipe examples provided by Apple for some code that might help you along the way.

[EDIT TO ADDRESS YOUR COMMENT BELOW]

If you want to go that simple then forget CoreData. Just create a plist for your assault rifles and make each of the assalt riffles a NSDictionary with two keys, one for the assault riffle name (NSString*) and one for its attachments (NSArray*). An example:

weapon1 {
          name: @"Weapon name here"
          attachments: [
                        item0: @"Attachment 1"
                        item1: @"Attachment 2"
                        item2: @"Attachment 3"
                        ...
                       ]
        }

weapon2 {
         ... // Keep adding weapon names & attachments
        }

You can then put weapon1, weapon2 etc in an NSArray to group them together. Say the weapons above are all assault riffles so this could become your AssaultRiffles plist that can be loaded and accessed using the standard KVC methods. Here's an example:

NSArray *weaponArray = // Method to load your plist here
for (NSDictionary *weapon in weaponArray)
{
    NSString *weaponName = [weapon objectForKey:@"name"];
    NSLog (@"weaponName is %@", weaponName);
}

If you don't want separate plists for each weapon type simple nest them all together in a dataset according to your needs - it can be either an array or dictionary.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜