【问题标题】:Initialize a state variable from observed object从观察到的对象初始化状态变量
【发布时间】:2020-02-28 13:15:01
【问题描述】:

我对可观察对象有另一个问题。

在“位置 1”行的视图中,我有用于编辑地址的文本视图(并将其保存到必须在视图中显示的状态属性“文本”中)。
状态变量“text”必须从类 LocationObserver 属性“currentAddress”初始化。 我只是试图设置视图的初始化 有人可以帮帮我吗?

提前致谢!

最好的

德拉甘


The View 


struct PickFoto: View {
    @ObservedObject var observeLocation = LocationObserver.shared()!
    @State private var address = LocationObserver.shared()?.currentAddress ?? " "

...    

TextView(text: $text, isEditing: $isEditing) // Position 1


Text("current Address (observeLocation): \(observeLocation.currentAddress)") // Position 2
...
}

The Shared Object

class  LocationObserver: NSObject, CLLocationManagerDelegate, ObservableObject {
    // This published property value  has to be the initial value   
    // for the TextView (the property $text. how to to this??)

    @Published var currentAddress = " " 

    static var instance: LocationObserver?

    var locationManager = CLLocationManager()
    var defaultLocation = CLLocation(latitude: 52.516861, longitude:13.356796)

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let currentLocation = locations.last?.coordinate



        convertGPSToAdress(coordinates: locations.last ?? defaultLocation,
                           completion: { (retStr: String) in
                                            self.currentAddress = retStr // set the  @Published property
                                            print("currentAddress in EscapingClosure: \(self.currentAddress)")
                                            //everything is fine
                                        }
        )

    }


    func convertGPSToAdress(coordinates: CLLocation,
                        completion:  @escaping(String) -> ()) -> () {

    let address = CLGeocoder.init()
    address.reverseGeocodeLocation(coordinates) { (places, error) in
        if error == nil{
            if places != nil {
                let place = places?.first
                completion(formatAddressString(place: place!)) // returns a formatted string address 
            }
        }
    }
}

    ....
}

【问题讨论】:

  • 你实现了自己的TextView视图吗?你能显示它的代码吗?
  • 您好 Aleksey,问题与 TextView 无关(如下所示)。最好的,德拉甘
  • 德拉甘,我错了,请见谅。

标签: state swiftui observedobject


【解决方案1】:

尝试以下(代码快照不可测试 - 所以只是刮擦)

struct PickFoto: View {
    @ObservedObject var observeLocation = LocationObserver.shared()!
    @State private var address: String

    init() {
        _address = State<String>(initialValue: LocationObserver.shared()?.currentAddress ?? " ")
    }

    ...

更新:将私有address 状态与外部同步

var body: some View {
    ...
    TextView(text: $text, isEditing: $isEditing) 
       .onReceive(observeLocation.$currentAddress) { newAddress in
            self.address = newAddress
       }
    ...
}

【讨论】:

  • 您好 Asperi,是的,这是初始化状态变量的正确方法。非常感谢!但现在我有一个类似的问题:变量地址未设置为“convertGPSToAdress”中计算的地址,因为转义闭包(标签“完成:”)在结构 PickFoto 中地址的“你的”初始化程序之后结束。任何想法?提前致谢!德拉甘
  • @user11646889,您可以通过订阅发布者来同步私有address状态,附加到正文中的任何视图,如更新的答案所示。
  • 嗨,Asperi,太好了!我只想初始化必须编辑的地址——它现在总是更新——不是一个大问题,我可以将它设置在闭包中,只有它没有被编辑。非常感谢,你解决了我的问题,如果你来柏林,第一杯啤酒就在我身上。PS 我可以在哪些文档中找到 SwiftUI 的这些方面?
  • @user11646889,很高兴听到。如果答案对您有用,那么在 SO 上投票并标记为已接受是一种良好的公民实践,以使其对其他有相同问题的社区更加可见。谢谢。
猜你喜欢
  • 2012-10-19
  • 1970-01-01
  • 2021-05-30
  • 1970-01-01
  • 2018-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多