How to save value in NSSet Core Data (Swift)
It's always easier to do it from the other end:
employees.company = company
Core data will take care of the many side of the relationship because you've specified the inverse relationship in your data model.
Also, rename your entity. It should be called Employee
, since each entity represents one employee. It will save you a lot of confusion down the road.
If you really insist on editing it from the company end, you do it like this:
company.mutableSetValueForKey("employees").addObject(employees)
Which I hope you'll agree is significantly uglier and more prone to errors.
How to use NSSet created from Core Data
Core Data supports widely native Swift types.
Declare codes
as Set<Codes>
in the Person
class.
It's much more convenient than typeless NSSet
.
You get a strong type and you can apply all native functions like filter
, sort
, etc. without type cast.
SwiftUI - Saving ARRAY into NSSet
Try the following instead of that line
if let categories = newQuestion.categories {
newQuestion.categories = categories.addingObjects(from: self.selections)
} else {
newQuestion.categories = Set(self.selections)
}
Why can't I save NSSet to 'to-many' relationship in coredata?
The most likely cause of this error as you report it is trying to add a set of the wrong class of objects. You can only add objects of the entity/class that is defined for the relationship in the data model.
So, if you have a data model like this:
Family{
name:string
people<-->>Person.family
}
Person{
name:string
family<<-->Family.people
}
... and you have NSManagedObject subclasses of FamilyMO
and PersonMO
, then you can only assign a set of PersonMO
objects to the Family.people
relationship. If you tried to assign a set like this:
NSSet *badSet=[NSSet setWithObjects:PersonMO1, PersonMO2, FamilyMO1, PersonMO2,nil];
aFamilyMO.people=badSet;
... it would fail and IIRC, it would do so silently.
Convert NSSet into an Array
My suggestion is to declare the relationship as non-optional native Swift type
@NSManaged public var children: Set<Node>
To get an array just sort
the set. This is pretty easy with a Swift Set
. As a set is unordered by definition you have to do that anyway to get a fixed order. Assuming you have a dateAdded
attribute you can write
let sortedChildren = children.sorted{$0.dateAdded > $1.dateAdded}
How to add to an NSSet using Core Data in Swift 5
When you add a relationship to an entity Xcode creates methods for getting and setting values for that relationship using a pre-defined naming standard. So you should have some methods in your Pokemon class for setting Type instances (and code completion should be able to help here):
addToTypes(value:) // single object
addToTypes(values:) //set of objects
So in your code it should be
pokemon.addToTypes(value: type)
You also have the same methods on Type for the opposite direction
Core Data relationship, NSSet & ManagedObjectContext
The usual way to get the members of the Set
object as array is the method allObjects
, but apart from that you cannot cast the TaskEntity
object to String
.
...
if let tasksArray = employeeRecord.tasks?.allObjects as? [TaskEntity] where !tasksArray.isEmpty {
cell.task1Label.text = tasksArray[0].taskName
cell.task1Label.text = tasksArray[1].taskName // or is it cell.task2Label ??
print(tasksArray[0].status)
}
return cell
Consider to rename the Core Data entities and their attributes. Actually the NSManagedObject
subclasses represent always an entity so it's redundant to include Entity
in the name. Or the attribute taskName
in TaskEntity
could be just name
. While reading the code you can infer that name
belongs to Task
Regarding the status
property you need to create an Int32
attribute statusValue
in the Task
entity to store the value in Core Data. The code to map the enumeration to Int32
and vice versa is fine.
Related Topics
Firebase Sms Verification on iOS - 'Token Mismatch'
Can You Evaluate a String in Swift
Programmatically Create an Nsviewcontroller Without an Xib in Swift 3
Swift Conditional Conformances with Generic Type
Uisearchbar's Set_Cancelbuttontext: Ivar Is Prohibited
Closures Return Value (Previously Completionblock)
How to Add Custom Init for String Extension
Need Detailed Explanation for Memoize Implementation in Swift (Wwdc 14, Session 404)
How to Get the Unicode Codepoint Represented by an Integer in Swift
Nsurlsessiondatadelegate Method Didreceivedata and Others Are Not Called
Passing and Storing Closures/Callbacks in Swift
iOS 13 Modals - Calling Swipe Dismissal Programmatically
Accessibility (Voice Over) with Sprite Kit
Using Animoji/Memoji as Profile Photo