2

i'm new to Realm and therefore i'm trying to test a Object however i keep getting the error below

Terminating app due to uncaught exception 'RLMException', reason: ''NSArray' is not supported as an RLMObject property. All properties must be primitives, NSString, NSDate, NSData, RLMArray, or subclasses of RLMObject. See http://realm.io/docs/cocoa/api/Classes/RLMObject.html for more information.

Object

import RealmSwift
import CoreLocation

class Organization: Object {
    var id: Int = 0
    var name: String = ""
    var image: NSData = NSData()
    var locations: [CLLocation] = []

}

Test in ViewDidLoad

    let organ1 = Organization()
    organ1.id = 1
    organ1.name = "Statens Museum For Kunst"
    organ1.image = UIImagePNGRepresentation(UIImage(named: "statensmuseum.jpg")!)!
    organ1.locations = [CLLocation(latitude: 50.6456604, longitude: 3.053486600000042)]

    // Persist your data easily
    let realm = try! Realm()
    try! realm.write {
        realm.add(organ1)
    }
1
  • From the documentation : "Realm supports the following property types: Bool, Int8, Int16, Int32, Int64, Double, Float, String, NSDate truncated to the second, and NSData. You can use List<Object> and Object to model relationships such as to-many and to-one. Subclassing Object is also supported." You cannot persist an Array this way, and I am not even sure you can persist CLLocation objects. You can do the trick by persisting the longitude and the latitude in two different Double object. Commented Sep 22, 2015 at 15:25

1 Answer 1

3

Here is a possible workaround to persist Location :

class Location: Object {
   var id: Int = 0
   var longitude: Double = 0
   var latitude: Double = 0
}

class Organization: Object {
   var id: Int = 0
   var name: String = ""
   var image: NSData = NSData()
   let locations: = List<Location>()
}

You can add a implement a constructor in Location which serialise CLLocation.

 let location1 = Location()
 //set your Location's properties

 let organ1 = Organization()
 organ1.id = 1
 organ1.name = "Statens Museum For Kunst"
 organ1.image = UIImagePNGRepresentation(UIImage(named: "statensmuseum.jpg")!)!
 organ1.locations.append(location1)
Sign up to request clarification or add additional context in comments.

1 Comment

So how will i add a Location object to the locations list? Could u give an example of this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.