I'm reading a key / value line text file in Swift Strings, one in five lines in the file lines. There are values separated from the place, which I have changed using the components to an array.
There are real values, so the common solution will be on the loop, each one and copy it into the respective slot in its strat's Int array. But am I missing some Swift (or Foundation) magic here?
You can use the map
function, which is the element of all the elements of the array And apply a change to each element defined at one closure.
var array = ["2", "4", "6", "8"] ints = array.map {$ 0.toInt ()! }
Note that I am using the open without compulsion to convert to integer-- use it if you are 100%, so make sure that all the elements in the array are actually integer Otherwise a runtime exception will arise.
It is to map an alternate integer in a safe way, then filter to remove zero values, and then force repeat options again. Experience:
IT = array.map {$ 0 .toInt ()} Kfiltr {$ 0! = Zero} .map {$ 0! }
Note that using this code for the protected version Appendix : As suggested by @mix, it is possible to use This looks like a good option for small size arrays, because it's O (3n) to O (2n) Reduces the complexity of - though I suspect that without compiler optimization it can be slow because a new array is created on each walk (if element is not zero), because loop can be slower than (and in fact it is because this array passes 3 - when Till the compiler is not able to customize it) - but I would love it on the loop because it is more compact and in my opinion is more readable, I do not have to use it for larger arrays Will do
reducing
to combine the last 2 steps of the secure version:
IT = array.map {$ 0.toInt ()} .reduce ([]) {$ 1! = Zero? $ 0 + [$ 1!]: $ 0}
$ 0 + [$ 1! ]
.
But it is good to know that there are several ways to get the same result :)
Comments
Post a Comment