从一个函数访问变量到另一个函数 Swift
Accessing variables from one function to another Swift
所以这似乎是一个简单的问题,可能有一个非常简单的解决方案,但一直无法解决。我在一个函数中形成了一个变量,我想在另一个函数中使用 'newPlace'、'place' 和 'place.name'...如何使它成为非局部变量?我想将 storedPlaces.append(newPlace) 放入另一个函数中,但它告诉我 newPlace 是一个未定义的变量……显然,当我将 let newPlace=.... 放在 class 下方时没有无法识别 'place'。我试着把 var place: GMSPlace?在顶部,但这也不起作用。
相关代码如下:
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
let camera = GMSCameraPosition.camera(withLatitude: place.coordinate.latitude, longitude: place.coordinate.longitude, zoom: 15.0)
self.vwGMap.camera = camera
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(place.coordinate.latitude, place.coordinate.longitude)
marker.title = place.name
marker.snippet = place.formattedAddress
marker.map = self.vwGMap
marker.icon = GMSMarker.markerImage(with: UIColor.blue)
marker.tracksViewChanges = true
self.dismiss(animated: true, completion: nil)
print("Place name: ", place.name)
print("Place address: ", place.formattedAddress)
print("Place placeID: ", place.placeID)
print("Place attributions: ", place.attributions)
print("Place Coordinate:", place.coordinate)
//The printing only happens in the terminal
let newPlace = StoredPlace(name: place.name, address: place.formattedAddress!, placeID: place.placeID)
storedPlaces.append(newPlace)
}
这是 class 的顶部:
var storedPlaces: [StoredPlace] = []
同样在 class 的顶部添加
var place:GMSPlace?
var newPlace:StoredPlace?
并在您的函数中分配它们
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
self.place = place
....
self.newPlace = StoredPlace(name: place.name, address: place.formattedAddress!, placeID: place.placeID)
}
所以这似乎是一个简单的问题,可能有一个非常简单的解决方案,但一直无法解决。我在一个函数中形成了一个变量,我想在另一个函数中使用 'newPlace'、'place' 和 'place.name'...如何使它成为非局部变量?我想将 storedPlaces.append(newPlace) 放入另一个函数中,但它告诉我 newPlace 是一个未定义的变量……显然,当我将 let newPlace=.... 放在 class 下方时没有无法识别 'place'。我试着把 var place: GMSPlace?在顶部,但这也不起作用。
相关代码如下:
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
let camera = GMSCameraPosition.camera(withLatitude: place.coordinate.latitude, longitude: place.coordinate.longitude, zoom: 15.0)
self.vwGMap.camera = camera
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(place.coordinate.latitude, place.coordinate.longitude)
marker.title = place.name
marker.snippet = place.formattedAddress
marker.map = self.vwGMap
marker.icon = GMSMarker.markerImage(with: UIColor.blue)
marker.tracksViewChanges = true
self.dismiss(animated: true, completion: nil)
print("Place name: ", place.name)
print("Place address: ", place.formattedAddress)
print("Place placeID: ", place.placeID)
print("Place attributions: ", place.attributions)
print("Place Coordinate:", place.coordinate)
//The printing only happens in the terminal
let newPlace = StoredPlace(name: place.name, address: place.formattedAddress!, placeID: place.placeID)
storedPlaces.append(newPlace)
}
这是 class 的顶部:
var storedPlaces: [StoredPlace] = []
同样在 class 的顶部添加
var place:GMSPlace?
var newPlace:StoredPlace?
并在您的函数中分配它们
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
self.place = place
....
self.newPlace = StoredPlace(name: place.name, address: place.formattedAddress!, placeID: place.placeID)
}