我需要将一个局部变量元组(sortedMarkers)从函数nearestMarker()复制到全局变量sortedMarkerGlobal中,但到目前为止它似乎并没有起作用。我想在另一个函数中使用sortedMarkersGlobal,但它的计数为零(空)。
class MapViewController: UIViewController {
@IBOutlet weak var mapView: GMSMapView!
var sortedMarkerGlobal: [(lat: String, long: String)] = []
var nextParkCount: Int = 1 // Used to iterate over sortedMarkers with nextPark IBAction button
let locationManager = CLLocationManager()
var lastLocation: CLLocation? = nil // Create internal value to store location from didUpdateLocation to use in func showDirection()
var isAnimating: Bool = false
var dropDownViewIsDisplayed: Bool = false
override func viewDidLoad() {
super.viewDidLoad()
....
}
func closestMarker(userLat: Double, userLong: Double)-> [(lat: String, long: String)] {
/*
var lengthRow = firstRow.count
while (sortedMarkers.count != lengthRow) { // There are markers that haven't been added to sortedMarkers
// Haversine formula
// add nearest marker to sortedMarkers
// Remove recently added marker from ParseViewControler() type tuple
// Recurse until all markers are in sortedMarkers
}
*/
let markerToTest = ParseViewController()
let firstRow = markerToTest.returnParse()
let lat2 = degreesToRadians(userLat) // Nearest lat in radians
let long2 = degreesToRadians(userLong) // Nearest long in radians
let R = (6371).doubleValue // Radius of earth in km
var closestLat: Double? = nil // Used to store the latitude of the closest marker
var closestLong: Double? = nil // Used to store the longitude of the closest marker
//
var indexToDelete: Int = 0 // Used to delete the marker which has been added to sortedMarkers tuple
let lengthRow = firstRow.count
var latLongs: (String, String)
var sortedMarkers: [(lat: String, long: String)] = []
var dynamicRow = firstRow // Tuple that has markers deleted from it
while (sortedMarkers.count != lengthRow) { // Store markers from closest to furtherst distance from user
var shortestDistance = 100.00E100
for (var i = 0; i < dynamicRow.count; i++) {
let testMarker = dynamicRow[i]
let testLat = (testMarker.lat as NSString)
let testLong = (testMarker.long as NSString)
let doubleLat = Double(testLat as String)
let doubleLong = Double(testLong as String)
let lat1 = degreesToRadians(doubleLat!)
let long1 = degreesToRadians(doubleLong!)
let dLat = lat2 - lat1
let dLong = long2 - long1
let a = ((sin((dLat)/2)) * (sin((dLat)/2))) + (cos(lat1)*cos(lat2)*((sin((dLong)/2)) * (sin((dLong)/2))))
let b = sqrt(a)
let d = (2*R) * (asin(b)) // Haversine formula
if (d < shortestDistance) {
closestLat = (doubleLat)
closestLong = (doubleLong)
shortestDistance = d
indexToDelete = i// Keep updating index of closest marker to later be removed
}
}
latLongs = (String(closestLat!), String(closestLong!)) // Each time for loop will find closest marker ~ NOT WORKING ~
sortedMarkers.append(latLongs)
dynamicRow.removeAtIndex(indexToDelete) // Remove marker that has just been added
}
sortedMarkerGlobal = sortedMarkers
return sortedMarkers
}
答案 0 :(得分:0)
sortedMarkerGlobal不是全局变量。目前它是一个实例变量,就像你定义的任何其他变量一样(locationManager,lastLocation,isAnimating等)。
如果您希望它是全局变量,则应在类MapViewController
之外定义它。
另一种选择是通过将其定义为:{/ p>,使其成为MapViewController
的类变量
class MapViewController: UIViewController {
static var sortedMarkerGlobal: [(lat: String, long: String)] = []
}
答案 1 :(得分:0)
好吧,我试图在游乐场测试你的代码,它对我有用
import UIKit
class Test {
var global:[(lat: String, long: String)] = []
func test1() {
var sortedMarkers: [(lat: String, long: String)] = []
var latLongs: (String, String)
latLongs = ("123", "123")
sortedMarkers.append(latLongs)
global = sortedMarkers
}
}
let test:Test = Test()
test.test1();
print("global \(test.global)")
结果是全局[(“123”,“123”)] 所以我想它也适用于你,问题可能在其他地方。
答案 2 :(得分:0)
我建议您只使用闭包而不是保存到全局变量。这允许您传入参数并根据它返回结果。您可以将该结果保存在调用它的函数中,并从那里执行任何操作。