gpt4 book ai didi

ios - 用数组添加 Realm 对象

转载 作者:可可西里 更新时间:2023-11-01 01:04:29 25 4
gpt4 key购买 nike

我是 Realm 的新手,因此我正在尝试测试一个对象,但是我一直收到以下错误

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

最佳答案

这是持久化 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>()
}

您可以在 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)

关于ios - 用数组添加 Realm 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32720744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com