无法访问Realm Object子类中的属性

时间:2017-12-14 21:28:34

标签: ios swift realm

我有一个领域对象子类,每次在地图上点击一个位置时都会实例化它。我的Realm Object子类是:

final class MapLocation:Object {
    @objc var text = ""
    @objc var latitude = Double()
    @objc var longitude = Double()
}

在我的地图视图控制器(GMSMapViewDelegate)中,我在每个地图上添加位置:

func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
    let lat = coordinate.latitude
    let lon = coordinate.longitude
    index += 1

    let marker = GMSMarker()
    marker.position = coordinate
    marker.map = mapView

    try! realm?.write {
        let location = MapLocation(value: ["latitude":lat, "longitude":lon, "text":"locaton-\(index)"])
        self.items.append(location)
        self.items.realm?.add(location)
    }
}

当此视图控制器加载时,我想显示在先前会话中创建的任何点。我这样做如下:

private func initMap(withPoints list:List<MapLocation>){
    for location in list {
        let newMarker = GMSMarker()
        let lat = location.latitude
        let lon = location.longitude
        newMarker.title = location.text
        newMarker.position = CLLocationCoordinate2D(latitude: lat, longitude: lon)
        newMarker.map = mapView
    }
}

在这个for循环中,我已经验证我正在检索MapLocation对象的持久列表。当我在这个循环中进行po print(location)时,我正确地得到以下内容:

MapLocation {
    text = locaton-1;
    latitude = 50.73600783337027;
    longitude = 4.882587678730488;
}

这是我的问题。我无法访问任何这些属性。例如:

(lldb) po print(location)
MapLocation {
    text = locaton-1;
    latitude = 50.73600783337027;
    longitude = 4.882587678730488;
}
(lldb) po print(location.longitude)
0.0
(lldb) po print(location.latitude)
0.0
(lldb) po print(location.text)
nil
(lldb) 

如此处所示,存储了属性,但在检索它们时,它们不可用。谁能告诉我为什么会这样?谢谢!

1 个答案:

答案 0 :(得分:3)

您需要将您的媒体资源标记为dynamic