简体   繁体   中英

adding realm object with array

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)
    }

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM