Was ist der richtige Typ für eine Entität?Xcode 8 (swift 3) Was ist der Typ einer Entität?
Wenn beispielsweise ein Wert für ein Attribut einer Entität gespeichert wird, welche Typdeklaration sollte die drei folgenden Fragezeichen ersetzen?
func setSample() {
let saveSample: <type???> = SampleEntity(context: context)
saveSample.sampleAttribute = "Save a new string."
do {
try context.save()
} catch {
print("Error with save: \(error)")
}
}
FWIW: NSEntityDescription gibt die Fehler:
Cannot convert value of type 'SampleEntity' to specified type 'NSEntityDescription'
Value of type 'NSEntityDescription' has no member 'sampleAttribute'
Context ich die Einheit in eine Variable machen möchten, so etwas zu tun:
func setSample() {
var saveSample: ???
if (criteria) { saveSample = Entity1(context: context) }
if (criteria) { saveSample = Entity2(context: context) }
if (criteria) { saveSample = Entity3(context: context) }
saveSample.sampleAttribute1 = "Foo"
saveSample.sampleAttribute2 = "Bar"
saveSample.sampleAttributeNth = "Etc"
do {
try context.save()
} catch {
print("Error with save: \(error)")
}
}
Wir brauchen mehr Kontext darüber, was genau Sie erreichen wollen und was die Deklarationen von "Entity1" und "Entity2" sind. – Alexander
@ Alexander Momchliov, "Entity1", "Entity2", "Entity3" sind die Entitätsnamen wie in CoreData.xcdatamodel aufgeführt. –