我正在尝试将位置数据保存在Realm中,我希望将其另存为新数据,但是当位置更改时以相同的名称保存。
下面是保存最后一个位置,但我希望它是每个位置
领域类
ConnectionRefused
ViewController类
SendAsync
didUpdateLocations
@objc dynamic var latitude = CLLocationDegrees()
@objc dynamic var longitude = CLLocationDegrees()
saveData函数
var myLocation = CLLocationCoordinate2D()
var lat = CLLocationDegrees()
var lon = CLLocationDegrees()
let data = Data()
我不知道我在错误的地方打电话给let theLocation = locations[0]
myLocation = CLLocationCoordinate2DMake(theLocation.coordinate.latitude, theLocation.coordinate.longitude)
saveData()
还是通过执行func saveData() {
do {
let realm = try Realm()
try realm.write {
data.latitude = myLocation.latitude
data.longitude = myLocation.longitude
realm.add(data)
}
} catch {
print("Error: \(error)"
}
}
来用新的经/纬度数据替换那里的数据?
答案 0 :(得分:2)
因为这里是1个实例
let data = Data()
您需要
func saveData() {
do {
let realm = try Realm()
let data = Data()
try realm.write {
data.latitude = myLocation.latitude
data.longitude = myLocation.longitude
realm.add(data)
}
} catch {
print("Error: \(error)"
}
}