Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift Can';t从firebase读取方法中提取var_Swift_Firebase - Fatal编程技术网

Swift Can';t从firebase读取方法中提取var

Swift Can';t从firebase读取方法中提取var,swift,firebase,Swift,Firebase,如何从Swift中Firebase的读取方法中获取numberOfMarkers? 如果我在{}中使用函数,这将保存,我将在{}中不使用它 docRef = Firestore.firestore().document("Markol/Markers") docRef.getDocument{ (docSnapshot, error) in guard let docSnapshot = docSnapshot, docSnapshot.exists else {return}

如何从Swift中Firebase的读取方法中获取
numberOfMarkers
? 如果我在
{}
中使用函数,这将保存,我将在
{}
中不使用它

docRef = Firestore.firestore().document("Markol/Markers")
docRef.getDocument{ (docSnapshot, error) in
    guard let docSnapshot = docSnapshot, docSnapshot.exists else {return}
    let myData = docSnapshot.data()
    let numberOfMarkers = myData?["NumberofMarkers"] as? Int ?? 0
}
//Here i want to get the let numberOfMarkers


    var markerArrayList = [GMSMarker]()
    func makeAMarker(_ Latitude:Double , _ Longitude:Double , _ Title:String,Snippet:String) -> GMSMarker{
        let GmMarker = GMSMarker()
        GmMarker.position = CLLocationCoordinate2D(latitude: CLLocationDegrees(Latitude), longitude: CLLocationDegrees(Longitude))
        GmMarker.title = Title
        GmMarker.snippet = Snippet
        GmMarker.icon = UIImage(named: "smallStoreIcon")
        return GmMarker
    }

getDocument
是一个,因此只有在关闭
}
之前才能访问
numberOfMarkers

使用
numberOfMarkers
getDocument
侦听器中执行任何您想要的操作,您可能需要重构现有代码以适应此情况。例如:

docRef = Firestore.firestore().document("Markol/Markers")
docRef.getDocument{ (docSnapshot, error) in
    guard let docSnapshot = docSnapshot, docSnapshot.exists else {return}
    let myData = docSnapshot.data()
    let numberOfMarkers = myData?["NumberofMarkers"] as? Int ?? 0
    processMarkers(numberOfMarkers, myData)
}

如果这种方法不清楚,试着在你的问题中发布更多的代码,这样其他人可以帮助你重组。

不,你不能。变量/常量总是在声明的范围内可见,在大括号
{…}
之间


您可能想做的是获取该值以返回它或在其他地方使用它。不要这样做,因为从
Firestore
获取数据是一项异步任务,请改用完成处理程序,并在有值时返回值(或
nil
,如果没有值),作为完成的参数

func call(completion: @escaping (Int?) -> Void) {
    ...
    docRef.getDocument{ docSnapshot, error in
        guard let docSnapshot = docSnapshot, docSnapshot.exists else {
            completion(nil)
            return 
        }
        let myData = docSnapshot.data()
        let numberOfMarkers = myData?["NumberofMarkers"] as? Int
        completion(numberOfMarkers)
    }
}
然后当你需要打电话的时候

call { numberOfMarkers in // code inside this closure is called with parameter of type `Int?` when you receive data and call completion from inside `call`
    if let number = numberOfMarkers {
        ... // do something with it
    }
}

。。。这里您可以将其用于下一个目的

有一种方法可以在{}的外侧设置一个新的var,并将其与numberOfMarkers相同?最后一行是做什么的?我建议你阅读我发送的链接。基本上,
getDocument{}
之外的任何东西都不能从内部读取值。如果您发布更多的代码,我们可以帮助您重新构造它。@OfekG在您的原始问题请!