Q:

Relationship in Core Data as NSSet

I have an entity of Category and an entity of Subcategory. it is one to many as one category can have many subcategories. I'm still trying to wrap my head around all this so bear with me...
extension Category {
    @NSManaged var name: String?
    @NSManaged var subCategory: SubCategory?
}

extension SubCategory {
    @NSManaged var name: String?
}

and then somewhere else in the code such as some view controller I make this call
let category = NSEntityDescription.insertNewObjectForEntityForName("Category", inManagedObjectContext: self.context) as! Category
let sub = SubCategory()
sub.name = "sub1"

category.subCategory = sub

//save it

And then when I look at the actual core data Sqlite db, I can see that the ZSUBCATEGORY column of the Category entity is populated with the primary key value of the sub category entity. I have defined the relationship in the data model.
My question is, I've noticed that some relationships are stored with a NSSet. Is there any benefit in doing this? Is what I'm doing wrong and egregious? To me this just seems simpler as I can then do...
category.subcategory.name 

to get the subcategory name

A:

If it was one to one relationship, indeed you could do this:
category.subcategory.name 

And that is what you are doing in this example, you have defined one to one relationship. If you want one to many than you would define it:
extension Category {
    @NSManaged var name: String?
    @NSManaged var subCategory: NSSet?
}

This way category is linked with number of sub categories, so if you would do
category.subcategory.name

how do you know which sub category you are accessing? You would have to iterate through set, to find one subcategory you want to find.

