gpt4 book ai didi

swift - 创建多行文本并将其写入文本文件 (CSV)?

转载 作者:行者123 更新时间:2023-11-30 10:42:32 24 4
gpt4 key购买 nike

我正在尝试创建一种用户登录并将其用户名和密码存储在文本文件中的方法。这是一个学校项目,因此它没有安全存储,我认为我没有时间设置/学习如何序列化数据。我遇到的主要问题是尝试将数据写入 CSV,以便稍后根据从文本文件接收到的数据将用户的分数拆分为数组。

我尝试了另一种写入文本文件的方法:
writeString.data(using: String.Encoding.utf8)?.write(to: fileURL, options: Data.WritingOptions.withoutOverwriting) 但这似乎对我不起作用

struct UserAccount: Codable {
var username: String
var password: String
var scores: [Int]
}

var user = UserAccount(username: "", password: "", scores: [0])

func writeTextFile(_ user: UserAccount) {
//Creating text file to read and write user's data (username, password, and score values)
let fileName = "UserDataQuizApp"
let dir = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)

do {
let fileManager = FileManager.default

if fileManager.fileExists(atPath: "UserDataQuizApp") {
try fileManager.removeItem(atPath: fileName)
}
} catch {print("error when deleting \(fileName)") }

// If the directory was found, we write a file to it and read it back
if let fileURL = dir?.appendingPathComponent(fileName).appendingPathExtension("txt") {
print("The path is: \(fileURL)")

do {
try user.username.write(to: fileURL, atomically: false, encoding: .utf8)
try ",".write(to: fileURL, atomically: false, encoding: .utf8)
try user.password.write(to: fileURL, atomically: false, encoding: .utf8)
for i in 0 ... user.scores.count {
try ",".write(to: fileURL, atomically: true, encoding: .utf8)
try String(user.scores[i]).write(to: fileURL, atomically: true, encoding: .utf8)
}
} catch {
print("Failed writing to URL: \(fileURL), Error: " + error.localizedDescription)
}
}
}

将数组写入文本文件现在根本不起作用,因为它说索引超出范围,但是当我将其注释掉并尝试仅写入用户名和密码时,只有密码会在我检查时的文件。我期望其中包含用户名、逗号和密码

最佳答案

崩溃原因符合 for i in 0 ... user.scores.count它应该是 for i in 0 ..< user.scores.countfor score in user.scores .

并且您正在尝试将每个文本写入文件。这意味着只有最后 write(to:..就会有效果。所有写入的内容都会被覆盖。

要解决此问题,请创建一个包含所有信息的字符串,然后将其写入文件。例如,写入函数应该类似于:

func writeTextFile(_ user: UserAccount) {
//Creating text file to read and write user's data (username, password, and score values)
let fileName = "UserDataQuizApp"
let dir = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)

do {
let fileManager = FileManager.default

if fileManager.fileExists(atPath: "UserDataQuizApp") {
try fileManager.removeItem(atPath: fileName)
}
} catch {print("error when deleting \(fileName)") }

// If the directory was found, we write a file to it and read it back
if let fileURL = dir?.appendingPathComponent(fileName).appendingPathExtension("txt") {
print("The path is: \(fileURL)")

do {
var contents = [user.username, user.password]
contents.append(contentsOf: user.scores.map { "\($0)"})
let string = contents.joined(separator: ",")
print("contents \(string)")
try string.write(to: fileURL, atomically: false, encoding: .utf8)
} catch {
print("Failed writing to URL: \(fileURL), Error: " + error.localizedDescription)
}
}
}

关于swift - 创建多行文本并将其写入文本文件 (CSV)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56479801/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com