2016-02-25 75 views
0

我需要將函數closestMarker()中的局部變量元組(sortedMarkers)複製到全局變量sortedMarkerGlobal中,但它似乎並沒有工作到目前爲止。我想在另一個函數中使用sortedMarkersGlobal,但它的計數是零(空)。本地變量不會複製到全局變量(swift)

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

請注意,儘管名稱「sortedMarkerGlobal」不是全局的, - 就這樣,你調試過了嗎?本地的'sortedMarkers'出來了嗎?如果是這樣,你有什麼證據表明'sortedMarkerGlobal'不是? – matt

+0

您應該使用兩種功能來簡化您的生活:排序和CLLocationDistance。使用這些,你可以使用大約一行代碼對你的數組進行排序。 – Michael

回答

0

sortedMarkerGlobal不是全局變量。目前它是一個實例變量,與您定義的其他變量(locationManager,lastLocation,isAnimating等)一樣。

如果你想要它是一個全局變量,你應該在你的類MapViewController之外定義它。

另一種選擇是由它定義爲使它的MapViewController類變量:

class MapViewController: UIViewController { 
    static var sortedMarkerGlobal: [(lat: String, long: String)] = [] 
} 
+0

感謝MapViewController以外的人定義它工作:) – z3st

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)") 

而且結果是global [(「123」,「123」)]所以我猜它也應該適合你,問題可能在其他地方。

0

我建議你只使用閉包而不是保存到全局變量。這可以讓你傳入參數並根據它返回結果。你可以將這個結果保存在調用它的函數中,並從那裏做你想做的事。