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

I want to write a string to a local file using writeToURL in swift

我有一个卡片数组,正面和背面都有文字。现在我想将这些数据存储在我创建并添加到我的项目中的 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块中添加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 协议
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)