当我尝试保存整数数组(即sets变量)时,会将\ n添加到数组中并使应用崩溃。这是我保存到核心数据的方式;集合看起来与此类似:[225,5,225,5]
func saveExercise(){
let exercise = Exercise(context: PersistanceManager.shared.context)
exercise.sets = sets
exercise.name = exerciseName.text
print(sets)
print(exercise)
PersistanceManager.shared.save()
}
运动实体:
import Foundation
import CoreData
extension Exercise {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Exercise> {
return NSFetchRequest<Exercise>(entityName: "Exercise")
}
@NSManaged public var name: String?
@NSManaged public var sets: [Int]?
}
PersistanceManager:
import Foundation
import CoreData
final class PersistanceManager {
private init() {}
static let shared = PersistanceManager()
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "King_of_the_Jungle")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
lazy var context = persistentContainer.viewContext
func save() {
if context.hasChanges {
do {
try context.save()
print("Saved Successfully!")
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
}
这是我的xc.datamodel的样子:
最后,这是我运行它时得到的输出: The error that occurs
然后输出print(sets),然后输出print(exercise): Print Statement Outputs