启动时执行函数

Execute function on startup

我正在尝试使用 swift 语言为 IOS 开发应用程序,这对我来说是个新闻。我想在应用程序启动时填写字典 (tobaccoList)。我有一个 csv 文件,所以我从这个文件中获取数据然后填充字典:

class DataManager{

var latitudes = Array<Double>()
var longitudes = Array<Double>()
var tobaccoList = Dictionary<Double, Tabacchino>()

init(){

    if let url = NSURL(fileURLWithPath: "/Users/brunopistone/Developer/apptabacchi/LocationList_sorted.csv" , isDirectory: true) {
        var error: NSErrorPointer = nil
        if let csv = CSV(contentsOfURL: url, error: error) {

            //put every tabbacchino in a Dictionary tobaccoList
            let rows = csv.rows
            let totalRows = rows.count

            for var index = 1; index < totalRows; index++ {
                let temp = csv.rows[index]
                let tabacchino = Tabacchino(
                    name: temp["Name"]!, phone: temp["tnumber"]!, lat: NSString(string: temp["Latitude"]!).doubleValue, lon: NSString(string: temp["Longitude"]!).doubleValue
                )
                let keyGeo = NSString(string: temp["Latitude"]!).doubleValue
                storeTobaccoShop(keyGeo, value: tabacchino)

                var doubleLatitude = NSString(string: temp["Latitude"]!).doubleValue
                var doubleLongitude = NSString(string: temp["Longitude"]!).doubleValue
                storeLatitude(doubleLatitude)
                storeLongitudes(doubleLongitude)
            }
        }
    }
}

func storeTobaccoShop(key: Double, value: Tabacchino) {
    self.tobaccoList[key] = value
}

在主页的 viewController 文件中我有:

class ViewController: UIViewController, CLLocationManagerDelegate {

let startFunction = DataManager()
let locationManager = CLLocationManager()
var latitude = Double()
var longitude = Double()
var tobaccoList = Dictionary<Double, Tabacchino>()

override func viewDidLoad() {

    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()

    tobaccoList = startFunction.getTobaccoList()

}

在主页中,我有一个调用另一个视图的按钮,我想将字典传递给另一个视图以便使用它,所以我使用了这个方法:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if segue.identifier == "tobaccoListSegue"{

        let viewList = segue.destinationViewController as! ViewList
        viewList.tabacchini = tobaccoList
    }
}

问题是,当我单击按钮以调用 viewList 时,应用程序再次填充字典。我想要的是仅在我打开应用程序时填写字典。 请帮我解决这个问题。谢谢

把这行放在

let startFunction = DataManager()

viewdidload() 方法内部。