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.

Monday, February 13, 2017

NSDateFormatter example

NSDateFormatter example

//get current time and date
let currentTime = NSDate()
let dateFormat = NSDateFormatter()
dateFormat.dateFormat = "yyyy/MM/dd HH:mm:ss"
print(dateFormat.stringFromDate(currentTime))

here is the chart which will be the most useful date field symbols

Format Description Example
"y" A year with 1 digit. 1 AD -> "1"
42 AD -> "42"
2014 AD -> "2014"
"yy" A year with exactly 2 digits.
If less then it will padded with 0.
It will be truncated to the tens digit if larger
1 AD -> "01"
42 AD -> "42"
2014 AD -> "14"
"yyy" A year with at least 3 digits.
If less then it will padded with 0.
1 AD -> "001"
42 AD -> "042"
2014 AD -> "2014"
"yyyy" A year with at least 3 digits.
If less then it will padded with 0.
1 AD -> "0001"
42 AD -> "0042"
2014 AD -> "2014"
"M" A month with at least 1 digits. June -> "6"
December -> "12"
"MM" A month with at least 2 digits.
If less then it will padded with 0.
June -> "06"
December -> "12"
"MMM" Three letter month abbreviation June -> "Jun"
December -> "Dec"
"MMMM" Full name of month June -> "June"
December -> "December"
"MMMMM" One letter month abbreviation
January, June and July are "J"
June -> "J"
December -> "D"
"d" A day with at least 1 digit 4 -> "4"
24 -> "24"
"dd" A day with at least 2 digit.
If less then it will padded with 0.
4 -> "04"
24 -> "24"
"E","EE" or "EEE" 3 letter day abbreviation of day name Wednesday -> "Wed"
Thursday -> "Thu"
"EEEE" Full day name Wednesday -> "Wednesday"
Thursday -> "Thursday"
"EEEEE" 1 letter day abbreviation of day name.
Teusday and Thursday are "T"
Wednesday -> "W"
Thursday -> "T"
"EEEEEE" 2 letter day abbreviation of day name Wednesday -> "We"
Thursday -> "Th"
"a" period of day (AM/PM) 7 PM -> "PM"
10 AM -> "AM"
"h" 12 hour based with at least 1 digit 7 PM -> "7"
10 AM -> "10"
"hh" 12 hour based with at least 2 digit.
If less then it will padded with 0.
7 PM -> "07"
10 AM -> "10"
"H" 24 hour based with at least 1 digit 7 PM -> "19"
10 AM -> "10"
"HH" 24 hour based with at least 2 digit.
If less then it will padded with 0.
7 PM -> "19"
9 AM -> "09"
"m" A minute with at least 1 digit 40 -> "40"
1 -> "1"
"mm" A minute with at least 2 digit.
If less then it will padded with 0.
40 -> "40"
1 -> "01"
"s" A second with at least 1 digit 40 -> "40"
1 -> "1"
"ss" A second with at least 2 digit.
If less then it will padded with 0.
40 -> "40"
1 -> "01"
"S" A tenth place of fraction second 123 ms -> "1"
7 ms -> "0"
"SS" A tenth and hundredths place of fractions second 123 ms -> "12"
7 ms -> "00"
"SSS" A tenth, hundredths and thousandths place of fractions second 123 ms -> "123"
7 ms -> "007"