更新时间:2022-05-27 21:39:31
只需尝试使用此
//MARK:- Extension for String
extension String
{
func appendLineToURL(fileURL: URL) throws
{
try (self + "\n").appendToURL(fileURL: fileURL)
}
func appendToURL(fileURL: URL) throws
{
let data = self.data(using: String.Encoding.utf8)!
try data.append(fileURL: fileURL)
}
}
//MARK:- Extension for File data
extension Data
{
func append(fileURL: URL) throws {
if let fileHandle = FileHandle(forWritingAtPath: fileURL.path)
{
defer
{
fileHandle.closeFile()
}
fileHandle.seekToEndOfFile()
fileHandle.write(self)
}
else
{
try write(to: fileURL, options: .atomic)
}
}
}
用法:
try newLine.appendToURL(fileURL: path!)
这些扩展将附加您正好想要的NewLine,
These Extension will Append Your NewLine Which you exactly looking for to Do,
这只会替换路径上的Line
try newLine.write(to: fileURL, atomically: false, encoding: .utf8)
这将使用现有数据作为新行附加数据
try newLine.appendToURL(fileURL: path!)