Jakob Jakob - 4 months ago 51
Swift Question

Error when integrating Realm

I'm trying to integrate Realm in my iOS app, so the data can be persistent.

Right now i'm getting this error:


Terminating app due to uncaught exception 'RLMException', reason:
'Property 'sections' is declared as 'NSArray', which is not a
supported RLMObject property type. All properties must be primitives,
NSString, NSDate, NSData, NSNumber, RLMArray, RLMLinkingObjects, or
subclasses of RLMObject. See
https://realm.io/docs/objc/latest/api/Classes/RLMObject.html for more
information.' First throw call stack:...


Can somebody tell me what i'm doing wrong?

The object class which is holding the
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
}
}

Answer

The error below just tell you that you can't assign certain types to Realm Objects, and specify the ones you can:

All properties must be primitives, NSString, NSDate, NSData, NSNumber, RLMArray, RLMLinkingObjects, or subclasses of RLMObject.

Replacing this line:

dynamic var sections:[String] = []

might solve your error. Although, the issue is in the way you are constructing the data. Look at the Sectioned tableView example. The table view section can be created dynamically when loading the view, there is no need to save it to the realm object.

If you do want to save the section maybe consider using another realm object to do so, for example:

class Sections: Object{
    dynamic var section: String = "" 
} 
Comments