I am trying to play with relationships in xCode and I'm having some problems:
I want to create a simple User to favoriteThing relationship. I created a model, so every user looks like this:
extension User {
@NSManaged var name: String?
@NSManaged var favoriteThings: NSSet?
}
extension FavoriteThing {
@NSManaged var thingName: String?
@NSManaged var user: User?
}
And I got a tableview conected (so it displays favoriteThing for each user).
The thing is, when I create a new favoriteThing how to add this thing to favoriteThings NSSet that is created in User class?
And what about when I delete this favoriteThing from my table view? (it is managed by fetchedResultsController) How to delete it also from mentioned favoriteThings NSSet?
Any help appreciated!
In the case of a one-to-many relationship, the easiest way to add an object to the to-many relationship is to set the property of the inverse to-one relationship:
theFavoriteThing.user = theUser
This automatically updates theUser.favoriteThings
to include theFavoriteThing
.
And to remove it just set the inverse to-one relationship to nil
:
theFavoriteThing.user = nil
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.