在CSV文件中的新行上添加新数据

时间:2021-09-26 08:11:59

I want to add a data from array to CSV file using https://github.com/yaslab/CSV.swift/issues by the way, the way they suggested was each new row need to declare

我想使用https://github.com/yaslab/CSV.swift/issues将数据从数组添加到CSV文件中,他们建议的方式是每个新行需要声明

try! csv.write(row: ["", ""])

manually, for instance.

例如,手动。

import Foundation
import CSV

let stream = OutputStream(toFileAtPath: "/path/to/file.csv", append: false)!
let csv = try! CSVWriter(stream: stream)

try! csv.write(row: ["id", "name"])
try! csv.write(row: ["1", "foo"])
try! csv.write(row: ["1", "bar"])

csv.stream.close()

Anyway, I need to make it create a new row automatically depends on the number of objects inside the array. So I'd changed it by append each csv into array

无论如何,我需要让它自动创建一个新行取决于数组内的对象数量。所以我通过将每个csv附加到数组中来改变它

    func saveCSVFiles() -> URL {
    let itemList = objectNo
    let fileManager = FileManager.default

        let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
        csvFile = documentDirectory.appendingPathComponent("\(fileName!).csv")
        let path = csvFile

        let stream = OutputStream(url: csvFile!, append: false)
        let csv = try! [CSVWriter(stream: stream!)]
        try! csv[0].write(row: ["name", "x", "y", "width", "height"])

        for no in stride(from: 1, to: itemList.count, by: 1) {
            try! csv[no].write(row: [String(describing: objects.dataName[no]), String(describing: objects.dataX[no]), String(describing: objects.dataY[no]), String(describing: objects.boundingX[no]), String(describing: objects.boundingY[no])])
    }
        csv[itemList.count].stream.close()

        print("yesss!", "\(fileName!)")
    return path!
}

but every time I tried to save it cause Fatal Error: index out of range on line for-loop and doesn't save at all

但每次我试图保存它会导致致命错误:索引超出范围的for-loop并且根本不保存

Any suggestion would be helpful

任何建议都会有所帮助

1 个解决方案

#1


0  

Arrays in Swift (and most languages) are zero-based. So you want your for loop to go from 0 to count-1. There is a built-in construct, ..<, thats perfect for that:

Swift(和大多数语言)中的数组是从零开始的。所以你希望你的for循环从0变为count-1。有一个内置的构造,.. <,这是完美的:

for no in 0..< itemList.count {
            try! csv[no].write(row: [String(describing: objects.dataName[no]), String(describing: objects.dataX[no]), String(describing: objects.dataY[no]), String(describing: objects.boundingX[no]), String(describing: objects.boundingY[no])])
}

EDIT

It sounds like you need to index into your csv array from 1 to itemList.count, and into all your other arrays from 0 to itemList.count - 1. Thus you should probably use

听起来你需要索引你的csv数组从1到itemList.count,并进入你从0到itemList.count的所有其他数组 - 1.因此你应该使用

for no in 0..< itemList.count {
            try! csv[no+1].write(row: [String(describing: objects.dataName[no]), String(describing: objects.dataX[no]), String(describing: objects.dataY[no]), String(describing: objects.boundingX[no]), String(describing: objects.boundingY[no])])
}

(note the +1 in the index into csv.)

(注意csv索引中的+1。)

#1


0  

Arrays in Swift (and most languages) are zero-based. So you want your for loop to go from 0 to count-1. There is a built-in construct, ..<, thats perfect for that:

Swift(和大多数语言)中的数组是从零开始的。所以你希望你的for循环从0变为count-1。有一个内置的构造,.. <,这是完美的:

for no in 0..< itemList.count {
            try! csv[no].write(row: [String(describing: objects.dataName[no]), String(describing: objects.dataX[no]), String(describing: objects.dataY[no]), String(describing: objects.boundingX[no]), String(describing: objects.boundingY[no])])
}

EDIT

It sounds like you need to index into your csv array from 1 to itemList.count, and into all your other arrays from 0 to itemList.count - 1. Thus you should probably use

听起来你需要索引你的csv数组从1到itemList.count,并进入你从0到itemList.count的所有其他数组 - 1.因此你应该使用

for no in 0..< itemList.count {
            try! csv[no+1].write(row: [String(describing: objects.dataName[no]), String(describing: objects.dataX[no]), String(describing: objects.dataY[no]), String(describing: objects.boundingX[no]), String(describing: objects.boundingY[no])])
}

(note the +1 in the index into csv.)

(注意csv索引中的+1。)