我是Realm的新手,因此我正在尝试测试一个Object但是我一直收到错误
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.
对象
import RealmSwift
import CoreLocation
class Organization: Object {
var id: Int = 0
var name: String = ""
var image: NSData = NSData()
var locations: [CLLocation] = []
}
在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)
}
答案 0 :(得分:3)
以下是保留位置的可能解决方法:
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>()
}
您可以在Location
中添加一个构造函数,该序列化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)