Showing posts with label swift 3. Show all posts
Showing posts with label swift 3. Show all posts

Saturday, September 2, 2017

Unit Conversion in Swift

Unit Conversion in Swift

Here is the sample code for how to use Measurement in Swift for kg to lb (i.e. Metric to Imperial )

var weight = 20.6
var weightToBeConvert = Measurement(value: weight, unit: UnitMass.kilograms)
weightToBeConvert.convert(to: UnitMass.pounds)
weight = weightToBeConvert.value

Measurement is available from iOS10 +

Monday, February 20, 2017

Sum of array in swift 3

Sum of array in swift 3

Let's say you have array of int and you want to total of all element.
Here is example,

        let ints = 1...10 // [1,2,3,4,5,6,7,8,9,10]
        print(ints.reduce(0, +))

After running this 2 lines of code you will get total of 1 to 10.