CSVImporter在viewdidload [Swift]之后开始导入 [英] CSVImporter starts importing after viewdidload [Swift]

查看:130
本文介绍了CSVImporter在viewdidload [Swift]之后开始导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从.csv文件导入数据,因此我使用了CSVImporter https://github. com/Flinesoft/CSVImporter .它运作良好,但在执行viewDidLoad函数的另一部分之前就开始导入.

I want to import data from a .csv file, so I have used the CSVImporter https://github.com/Flinesoft/CSVImporter. It works well, but it starts the importing before the other part of the function viewDidLoad is executed.

以下代码仅是测试,但我需要一个解决方案以确保CSVImporter在其他viewDidLoad代码执行之前完成导入,或者需要一个在viewDidLoad之后自动启动的功能.

The following code is only a test but I need either a solution that ensures that the CSVImporter completes importing before the other viewDidLoad code executes or a function which starts automatically after viewDidLoad.

这是我的代码:

var Vokabeln: [[String]]?


var i = 0


override func viewDidLoad() {
    super.viewDidLoad()


    let path = "/Users/---CENSORED---/Documents/TestLöschen/TestLöschen/Vokabeln.csv"
    let importer = CSVImporter<[String]>(path: path, delimiter: ";")
    importer.startImportingRecords { $0 }.onFinish { importedRecords in
        for record in importedRecords {
            self.Vokabeln?[self.i][0] = record[0]
            self.Vokabeln?[self.i][1] = record[1]
            self.Vokabeln?[self.i][2] = record[2]
            print("Begin1")
            print(record[0])
            print(record[1])
            print(record[2])
            print("End1")
            self.i += 1
        }
    }

    print("Begin2")
    print(Vokabeln?[0][0])
    print(Vokabeln?[0][1])
    print(Vokabeln?[0][2])
    print(Vokabeln?[1][0])
    print(Vokabeln?[1][1])
    print(Vokabeln?[1][2])
    print("End2")
}

因此,首先打印"Begin2",并打印6次nil.然后,当函数viewDidLoad完成时,它将显示"Begin1",然后显示正确的变量和"End1"

So first it prints "Begin2" and 6 times prints nil. Then, when the function viewDidLoad is finished, it prints "Begin1", then the correct variables and "End1"

有人可以帮助我吗? 谢谢.

Can anybody help me? Thanks.

推荐答案

startImportingRecords异步工作,只需在重复循环后将代码用于在完成处理程序中打印Vokabeln即可.

startImportingRecords works asynchronously, just put the code to print the Vokabeln in the completion handler after the repeat loop.

首先,您需要初始化数组,否则将不附加任何内容.并且不要将数组声明为可选,变量名应该以小写字母开头.

First of all you need to initialize the array otherwise nothing will be appended. And do not declare the array as optional and variable names are supposed to start with a lowercase letter.

var vokabeln = [[String]]()

例如,如果要更新UI,请将代码包装在一个调度块中

In case to update the UI wrap the code in a dispatch block for example

importer.startImportingRecords { $0 }.onFinish { importedRecords in
    for record in importedRecords {
        self.vokabeln[self.i][0] = record[0]
        self.vokabeln[self.i][1] = record[1]
        self.vokabeln[self.i][2] = record[2]
        print("Begin1")
        print(record[0])
        print(record[1])
        print(record[2])
        print("End1")
        self.i += 1
    }
    DispatchQueue.main.async {
        print("Begin2")
        print(self.vokabeln[0][0])
        print(self.vokabeln[0][1])
        print(self.vokabeln[0][2])
        print(self.vokabeln[1][0])
        print(self.vokabeln[1][1])
        print(self.vokabeln[1][2])
        print("End2")
   }
}

但是还有另一个问题.如果将数组声明为[[String]],则外部和内部数组均为空,并且无法使用索引下标分配值.我推荐这种语法

But there is still another issue. If you declare the array as [[String]] both outer and inner arrays are empty and you cannot assign values with index subscripting. I recommend this syntax

for record in importedRecords {
    self.vokabeln.append(record) // this appends the whole record array
    print("Begin1")
    print(record)
    print("End1")
}

PS:考虑使用更合适的文本格式,例如JSON或属性列表.

PS: Consider to use a more suitable text format like JSON or property list.

这篇关于CSVImporter在viewdidload [Swift]之后开始导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆