Swift - Write an Array to a Text File -
I read in myArray (native swift) from a file containing a few thousand lines of plain text
myData = String.stringWithContentsOfFile (myPath, Encoding: NSUTF8StringEncoding, Error: Zero) var myArray = myData.componentsSeparatedByString ("\ n")
I change some of the text in MyArray ( No point to paste this code any).
Now I want to add the updated content of my array to a new file. I have tried this.
Give myArray2 = myArray as NSArray myArray2.writeToFile (myPath, atomically: false)
but file content Then in Plist's format
Is there any way in Swift (or Bridged Swift) by inserting a string array into a file (or loop through an array and adding each array item to a file)?
You have to reduce your array back to the string:
Var output = decrease (array, "") in current {{current, toAppend}, return to {ifApp} {return toAppend} and {return "\ (existing) \ n \ (toAppend)"}} output.writeToFile (. ..)
decrease
method collects a collection and merges everyone into one instance, it takes the initial example, and in that original frequency all the elements of the collection Closes to merge.
My example takes an empty string as an initial example. Closing checks if the current output is empty or not. If so, then only to return the text, it must be returned, otherwise, it uses the current output and the new element to return with a new line in the middle.
Output = low (array, "") {$ 0}
.isEmpty? $ 1: "\ ($ 0) \ n \ ($ 1)"}
Comments
Post a Comment