I read into myArray (native Swift) from a file containing a few thousand lines of plain text..
我从一个包含数千行纯文本的文件中读取myArray。
myData = String.stringWithContentsOfFile(myPath, encoding: NSUTF8StringEncoding, error: nil)
var myArray = myData.componentsSeparatedByString("\n")
I change some of the text in myArray (no point pasting any of this code).
我更改了myArray中的一些文本(没有任何一点粘贴此代码)。
Now I want to write the updated contents of myArray to a new file. I've tried this ..
现在我想将myArray的更新内容写入一个新文件。我已经试过这个. .
let myArray2 = myArray as NSArray
myArray2.writeToFile(myPath, atomically: false)
but the file content is then in the plist format.
但是文件内容是plist格式的。
Is there any way to write an array of text strings to a file (or loop through an array and append each array item to a file) in Swift (or bridged Swift)?
有没有办法用Swift(或桥接Swift)将文本字符串数组写入文件(或通过数组循环并将每个数组项附加到文件)?
3 个解决方案
#1
2
You need to reduce your array back down to a string:
您需要将数组还原为字符串:
var output = reduce(array, "") { (existing, toAppend) in
if existing.isEmpty {
return toAppend
}
else {
return "\(existing)\n\(toAppend)"
}
}
output.writeToFile(...)
The reduce
method takes a collection and merges it all into a single instance. It takes an initial instance and closure to merge all elements of the collection into that original instance.
reduce方法接受一个集合并将其合并到一个实例中。它使用初始实例和闭包将集合的所有元素合并到原始实例中。
My example takes an empty string as its initial instance. The closure then checks if the existing output is empty. If it is, it only has to return the text to append, otherwise, it uses String Interpolation to return the existing output and the new element with a newline in between.
我的示例将一个空字符串作为初始实例。然后,闭包检查现有输出是否为空。如果是,它只需要将文本返回到append,否则,它将使用字符串插值来返回现有的输出和在中间的换行符的新元素。
Using various syntactic sugar features from Swift, the whole reduction can be reduced to:
使用Swift的各种句法糖特征,整体还原为:
var output = reduce(array, "") { $0.isEmpty ? $1 : "\($0)\n\($1)" }
#2
17
As drewag points out in the accepted post, you can build a string from the array and then use the writeToFile
method on the string.
正如drewag在接受的post中指出的那样,您可以从数组中构建一个字符串,然后在字符串上使用writeToFile方法。
However, you can simply use Swift's Array.joinWithSeparator
to accomplish the same with less code and likely better performance.
但是,您可以使用Swift的数组。连接分隔符以更少的代码完成同样的工作,并且可能有更好的性能。
For example:
例如:
// swift 2.0
let array = [ "hello", "goodbye" ]
let joined = array.joinWithSeparator("\n")
do {
try joined.writeToFile(saveToPath, atomically: true, encoding: NSUTF8StringEncoding)
} catch {
// handle error
}
// swift 1.x
let array = [ "hello", "goodbye" ]
let joined = "\n".join(array)
joined.writeToFile(...)
#3
-1
Swift offers numerous ways to loop through an array. You can loop through the strings and print to a text file one by one. Something like so:
Swift提供了许多遍历数组的方法。您可以对字符串进行循环,并将其逐个打印到文本文件中。类似这样:
for theString in myArray {
theString.writeToFile(myPath, atomically: false, encoding: NSUTF8StringEncoding, error: nil);
}
#1
2
You need to reduce your array back down to a string:
您需要将数组还原为字符串:
var output = reduce(array, "") { (existing, toAppend) in
if existing.isEmpty {
return toAppend
}
else {
return "\(existing)\n\(toAppend)"
}
}
output.writeToFile(...)
The reduce
method takes a collection and merges it all into a single instance. It takes an initial instance and closure to merge all elements of the collection into that original instance.
reduce方法接受一个集合并将其合并到一个实例中。它使用初始实例和闭包将集合的所有元素合并到原始实例中。
My example takes an empty string as its initial instance. The closure then checks if the existing output is empty. If it is, it only has to return the text to append, otherwise, it uses String Interpolation to return the existing output and the new element with a newline in between.
我的示例将一个空字符串作为初始实例。然后,闭包检查现有输出是否为空。如果是,它只需要将文本返回到append,否则,它将使用字符串插值来返回现有的输出和在中间的换行符的新元素。
Using various syntactic sugar features from Swift, the whole reduction can be reduced to:
使用Swift的各种句法糖特征,整体还原为:
var output = reduce(array, "") { $0.isEmpty ? $1 : "\($0)\n\($1)" }
#2
17
As drewag points out in the accepted post, you can build a string from the array and then use the writeToFile
method on the string.
正如drewag在接受的post中指出的那样,您可以从数组中构建一个字符串,然后在字符串上使用writeToFile方法。
However, you can simply use Swift's Array.joinWithSeparator
to accomplish the same with less code and likely better performance.
但是,您可以使用Swift的数组。连接分隔符以更少的代码完成同样的工作,并且可能有更好的性能。
For example:
例如:
// swift 2.0
let array = [ "hello", "goodbye" ]
let joined = array.joinWithSeparator("\n")
do {
try joined.writeToFile(saveToPath, atomically: true, encoding: NSUTF8StringEncoding)
} catch {
// handle error
}
// swift 1.x
let array = [ "hello", "goodbye" ]
let joined = "\n".join(array)
joined.writeToFile(...)
#3
-1
Swift offers numerous ways to loop through an array. You can loop through the strings and print to a text file one by one. Something like so:
Swift提供了许多遍历数组的方法。您可以对字符串进行循环,并将其逐个打印到文本文件中。类似这样:
for theString in myArray {
theString.writeToFile(myPath, atomically: false, encoding: NSUTF8StringEncoding, error: nil);
}