gpt4 book ai didi

swift - 我想在 swift 中使用 writeToURL 将字符串写入本地文件

转载 作者:搜寻专家 更新时间:2023-11-01 06:39:13 25 4
gpt4 key购买 nike

我有一个卡片数组,正面和背面都有文字。现在我想将这些数据存储在我创建并添加到我的项目中的 csv 文件中。以下代码创建以逗号分隔的字符串数组,然后尝试将其写入文件。但是文件仍然是空的。代码中没有显示错误。知道我在这里做错了什么吗?

let path = NSBundle.mainBundle().pathForResource("cardsFile", ofType:"csv")
for i in 0...cards.count - 1{
let card = [cards[i].front,cards[i].back]
let cardRow:String = card.joinWithSeparator(",")
print(cardRow)///confirmed - carRow is not empty
do {
try cardRow.writeToURL(NSURL(fileURLWithPath: path!), atomically: false, encoding: NSUTF8StringEncoding)
}
catch {}
}

最佳答案

没有显示任何错误,因为您捕获了错误但没有做任何事情。在catch block 中添加print(error):

do {
try cardRow.writeToURL(NSURL(fileURLWithPath: path!), atomically: false, encoding: NSUTF8StringEncoding)
}
catch {
print(error)
}

问题是您正在尝试写入包中的文件,但包是只读的。您应该尝试写入文档文件夹:

let fileURL = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false)
.URLByAppendingPathComponent("cardsFile.csv")

顺便说一句,writeToFile 将替换输出文件。如果您想将数据写入文件,并在进行过程中追加数据,您可能需要使用 NSOutputStream:

let fileURL = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false).URLByAppendingPathComponent("cardsFile.csv")

guard let stream = NSOutputStream(URL: fileURL, append: false) else {
print("unable to open file")
return
}

stream.open()

for card in cards {
let data = "\(card.front),\(card.back)\n".dataUsingEncoding(NSUTF8StringEncoding)!
guard stream.write(UnsafePointer<UInt8>(data.bytes), maxLength: data.length) > 0 else {
print("unable to write to file")
break
}
}

stream.close()

如果目标只是将其保存在本地,我不会使用 CSV 格式。我会改用原生格式,例如 NSKeyedArchiver。因此,首先通过实现 init?(coder:)encodeWithCoder() 使 Card 符合 NSCoding 协议(protocol):

class Card: NSObject, NSCoding {
let front: String
let back: String

init(front: String, back: String) {
self.front = front
self.back = back
super.init()
}

required convenience init?(coder aDecoder: NSCoder) {
guard let front = aDecoder.decodeObjectForKey("front") as? String else { return nil }
guard let back = aDecoder.decodeObjectForKey("back") as? String else { return nil }
self.init(front: front, back: back)
}

func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(front, forKey: "front")
aCoder.encodeObject(back, forKey: "back")
}

override var description: String { get { return "<Card front=\(front); back=\(back)>" } }
}

然后,当你想保存时:

let fileURL = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false)
.URLByAppendingPathComponent("cardsFile.bplist")

if !NSKeyedArchiver.archiveRootObject(cards, toFile: fileURL.path!) {
print("error saving archive")
}

当你想从文件中读取它时:

guard let cards2 = NSKeyedUnarchiver.unarchiveObjectWithFile(fileURL.path!) as? [Card] else {
print("problem reading archive")
return
}

print(cards2)

关于swift - 我想在 swift 中使用 writeToURL 将字符串写入本地文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37649599/

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