Hi,
At first I am a beginner in swift and i hope that you can understand my english 😉.
I want to save a struct but I dont know how. I try to use the "CoreData" but it doesn´t work.
Here my struct:
struct Section {
var heading : String
var items : [String]
var foto : [UIImage?]
var beschreibung: [String]
var genre: [String]
var listeZutat: [[String]]
var listeEinheit: [[String]]
var listeMenge: [[Int]]
init(title: String, objects : [String], bild : [UIImage?], rezeptbeschreibung: [String], rezeptGenre: [String], ListeZutat: [[String]], ListeEinheit: [[String]], ListeMenge: [[Int]]) {
heading = title
items = objects
foto = bild
beschreibung = rezeptbeschreibung
genre = rezeptGenre
listeMenge = ListeMenge
listeZutat = ListeZutat
listeEinheit = ListeEinheit
}
}Here my code
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context: NSManagedObjectContext = appDel.managedObjectContext
let newMeal = NSEntityDescription.insertNewObjectForEntityForName("Rezepte", inManagedObjectContext: context) // Rezepte is my Entity in .xcdatamodeld
newMeal.setValue(sections, forKey: "rezepte") // sections is my struct, rezepte is my attribute in Enity
do{
try context.save()
}
catch{
print("Error")
}
do {
let request = NSFetchRequest(entityName: "Rezepte")
let result = try context.executeFetchRequest(request)
if result.count > 0 {
for item in result as! [NSManagedObject]{
let testLoad = item.valueForKey("rezepte")
print(testLoad)
}
}
}catch{
print("Error")
}// end catchThe error is in line 06: "Cannot convert value of type (section) to expected argument type AnyObject?"
In my .xcdatamodeld for my enity I can´t chose a struct for my DataCore only String,Double.....
I know that my Struc is not a type of AnyObject?. But I cant make my struct to AnyObject so how can I save a struct and is it possible to save a struct in a file?
Can someone give me a tipp how I can save the struct?
Bye Bye
Christian