我在Swift中使用Realm数据库保存数据时遇到问题。
我有两个实体叫做#34; MathSolutions" ,"文件夹"。一个文件夹可以有多个问题。一对多关系。
MathSolutions实体
class MathSolutions: Object {
dynamic var question = ""
dynamic var steps = ""
dynamic var createdDate = NSDate()
let solutions = List<Folders>()
class func JSONInboundMappingDictionary() -> [String:String] {
return [
//"id": "id",
"question" : "question",
"steps" :"steps",
"createdDate" : "createdDate",
"solutions" : "solutions"
]
}
}
文件夹类
class Folders: Object {
dynamic var folderName: String = ""
var folders = LinkingObjects(fromType: MathSolutions.self, property: "solutions")
}
另一个处理部分
let mathSolution = MathSolutions(value: ["question": answers.question,"steps":realmcrud.createJOSNString(answers),"createdDate":NSDate()] )
realmcrud.saveMySolution(mathSolution,folder: folder!)
//MARK:-Save Solutions
func saveMySolution(mathSolutions:MathSolutions,folder: Folders){
let realm = try! Realm()
try! realm.write{
mathSolutions.solutions.append(folder)
realm.add(mathSolutions)
}
}
创建的文件夹工作正常。问题是当我保存MathSolutions
时,我得到了以下错误。
我总是低于错误
2016-10-12 15:39:36.234 JIT[92648:1964826] *** Terminating app due to uncaught exception 'Invalid property name', reason: 'Property 'name' not found in object of type 'Folders''
调用创建部分
let realmcrud = RealmCURD();
//let folder = Folders(value : ["id" : realmcrud.folderNextID(),"folderName":self.enteredFolderName])
let folder = Folders(value : ["folderName":self.enteredFolderName])
realmcrud.createFolder(folder)
创建文件夹数据库文件
//MARK:-Create folder
func createFolder(folders: Folders){
let realm = try! Realm()
try! realm.write{
realm.add(folders,update: false)
}
}
我的需求是我有文件夹&amp; MathSolution实体, - 我有文件夹列表或用户可以创建新文件夹,此后他们可以从列表中保存我选择的文件夹中的数学问题。
id
和{{Folders
中有MathSolution
个文件1}}实体类,使用id给出文件夹引用,即当我保存问题时,它会转到像MathSolution
这样的question , steps , id, createdDate
表,就像文件夹表&#39; id, folderName
] 但后来我决定在遇到此问题时使用对象映射。清楚吗?任何解决方案请知道吗?请建议