我正在尝试将Realm集成到我的iOS应用中,以便数据可以保持持久性。
现在我收到此错误:
由于未捕获的异常'RLMException'而终止应用程序,原因:'属性'sections'被声明为'NSArray',这不是受支持的RLMObject属性类型。所有属性都必须是原语,NSString,NSDate,NSData,NSNumber,RLMArray,RLMLinkingObjects或RLMObject的子类。有关更多信息,请参见 https://realm.io/docs/objc/latest/api/Classes/RLMObject.html。首先抛出调用堆栈:...
有人可以告诉我我在做什么错吗?
持有的对象类sections。
class Workout: Object {
dynamic var image: String = ""
dynamic var name: String = ""
dynamic var type: String = ""
dynamic var sections:[String] = []
var dayOne = List<Exercise>()
var dayTwo = List<Exercise>()
var dayThree = List<Exercise>()
var dayFour = List<Exercise>()
var dayFive = List<Exercise>()
func addExerciseToSection(sectionName: String, exerciseName: Exercise) {
if sectionName == "Day 1" {
dayOne.append(exerciseName)
} else if sectionName == "Day 2" {
dayTwo.append(exerciseName)
} else if sectionName == "Day 3" {
dayThree.append(exerciseName)
} else if sectionName == "Day 4" {
dayFour.append(exerciseName)
} else if sectionName == "Day 5" {
dayFive.append(exerciseName)
}
}
func getWorkoutInSection(workout: Workout, section: Int) -> List<Exercise>? {
if section == 0 {
return workout.dayOne
} else if section == 1 {
return workout.dayTwo
} else if section == 2 {
return workout.dayThree
} else if section == 3 {
return workout.dayFour
} else if section == 4 {
return workout.dayFive
}
return nil
}
}
Run Code Online (Sandbox Code Playgroud)
以下错误仅告诉您您不能将某些类型分配给Realm Objects,并指定可以的类型:
所有属性都必须是原语,NSString,NSDate,NSData,NSNumber,RLMArray,RLMLinkingObjects或RLMObject的子类。
替换此行:
dynamic var sections:[String] = []
Run Code Online (Sandbox Code Playgroud)
可能会解决您的错误。虽然,问题出在构建数据的方式上。查看Sectioned tableView示例。加载视图时,可以动态创建表视图部分,而无需将其保存到领域对象。
如果确实要保存,则section可以考虑使用另一个领域对象来保存,例如:
class Sections: Object{
dynamic var section: String = ""
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3370 次 |
| 最近记录: |