Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.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
Ios 启动时执行函数_Ios_Xcode_Swift_Dictionary_Viewcontroller - Fatal编程技术网

Ios 启动时执行函数

Ios 启动时执行函数,ios,xcode,swift,dictionary,viewcontroller,Ios,Xcode,Swift,Dictionary,Viewcontroller,我正在尝试使用swift语言为IOS开发一个应用程序,这对我来说是个新闻。我想在应用程序启动时填写字典(烟草列表)。我有一个csv文件,因此我从该文件中获取数据,然后填充字典: class DataManager{ var latitudes = Array<Double>() var longitudes = Array<Double>() var tobaccoList = Dictionary<Double, Tabacchino>() init()

我正在尝试使用swift语言为IOS开发一个应用程序,这对我来说是个新闻。我想在应用程序启动时填写字典(烟草列表)。我有一个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
}
问题是,当我单击按钮调用viewList时,应用程序会再次填充字典。我只想在打开应用程序时填写字典。 请帮我修一下这个东西。谢谢

把这句话放好

let startFunction = DataManager()

在viewdidload()方法内部。

是否在其他任何地方创建新的
DataManager
?您的代码只显示创建一次。不相关,但您在
viewdiload
中缺少对super的调用。不,与DataManager相关的内容只在我之前编写的代码段中完成。您的代码显示,创建
DataManager
时,它会解析文件。因此,如果创建两次,则最终将加载两次文件。
gettobaccomist()
中的代码是什么样子的?
gettobaccomist()
返回初始化
DataManager()
时创建的字典。我只在
类ViewController
中创建了一个变量DataManager,是什么让您相信字典被分配了多次?
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if segue.identifier == "tobaccoListSegue"{

        let viewList = segue.destinationViewController as! ViewList
        viewList.tabacchini = tobaccoList
    }
}
let startFunction = DataManager()