Testing interger multiples
Output:
It's multiple of 5
Escaping Raw strings
var rawText = #"""
Hey! I can write anything "here"
without using double double quotes \#(someNumber)
"""#
Output:
Hey! I can write anything "here"
without using double double quotes 10
isNumber to check a char value
var text = "SK10"
for char in text {
print("Is '\(char)' number? \(char.isNumber)")
}
Output:
Is 'S' number? false
Is 'K' number? false
Is '1' number? true
Is '0' number? true
Similarly, isASCII
, isLetter
, isSymbol
, isSymbol
etc. are available. Go ahead & give a try.
Use ‘KeyValuePairs’ instead ‘DictionaryLiteral’
// Swift 4.2 Code
let data: DictionaryLiteral = ["name": "Sagar", "post": "iOS App Dev"]
print(data)
// Swift 5 code
let newData: KeyValuePairs = ["name": "Sagar", "post": "iOS App Dev"]
print(newData)
Output:
["name": "Sagar", "post": "iOS App Dev"]
["name": "Sagar", "post": "iOS App Dev"]
compactMapValues
In following example, with compactMapValues
, I was able to convert [String: String]
to [String: Int]
let someData = ["avacado": "five", "banana": "10", "mango": "5", "lime": "30"]
let values = someData.compactMapValues(Int.init)
print("Values are \(values)")
Output:
Values are ["mango": 5, "lime": 30, "banana": 10]
Future Enum cases
enum Vehicle {
case car, bike, truck
}
let myVehicle: Vehicle = .car
switch myVehicle {
case .car:
print("Driving a car")
case .bike:
print("Riding a bike")
@unknown default:
print("Driving a vehicle")
}