Table of Contents
ToggleBoolean Toggling
Now it is easy to toggle the boolean value just by calling the function.toggle() function can be used to change the boolean state of the variable from true to false and vice versa.
Try this:
var userPromoCode = false
userPromoCode.toggle()
Here after the execution of the code the value of userPromoCode will be True.toggle() function help in means of readability and easiness if your write complex data structure.
Random Method
For randomizing the number we use C API method arc4random_uniform(10) till now, which is included in foundation library.Now swift has its own function to do this random() function can be used to generate a range of the random number on whatever numeric type you like. Randomization is not limit to numeric type collection can also be use it .shuffle() and shuffled() are two methods can be used for collections to shuffle the content of the collection safely.
Try this:
let random_Num = Int.random(in:0 ..< 10)
let random_Float = Float.random(in:0..< 10)
For Collections.
var diceRoll = [1,2,3,4,5,6]
diceRoll.shuffle()
print(“Dice Rolled \(diceRoll)”)
let itemShuffled = diceRoll.shuffled() //Returns shuffled array
print(“Dice Rolled \()”)
Sequence Methods
Any one with Java back ground will love this new sequence method in swift .Sequence Methods are used for find the element,Index of match element and direct index of the elements.
last(where:) , lastIndex(where:) , lastIndex(of:) are the methods introduced in swift out which two requires a closure as an argument and last requires the elements to be searched.
Try this.
1.let numberArr = [30,50,60,10,5,12,46,67]
print(“Last Highest Number \(numberArr.last(where:{$0 > 50}))”)
// Prints 67
$0 is the first value compare with the other elements
print(“Last Highest Number \(numberArr.lastIndex(where:{$0 > 50}))”)
// Prints 7
print(“Last Highest Number \(numberArr.astIndex(of:60))”)
// Prints 2
Sequence method not yet done we have allSatisfy(_:) that validate every element and return true if all match a given condition.
let numArr = [30,50,60,10,4,12,46,68]
let isEvenArr = numArr.allSatisfy({$0 % 2 == 0 })
// isEvenArr hold True value.
removeAll(where:) method remove all items in collection which matches the condition.
Try this
Let empName = [“sam”,”Rocky”,”Smith”,”logan”]
empName.removeAll{$0.hasSuffix(“ky”)}
print(empName)
//Prints sam,logan,smith
Dynamic Member Lookup
Dynamic member lookup allows swift to call a subscript method when accessing properties. It is the easiest way of accessing property just by using dot operator . Once you register your class/struct with @dynamicMemberLookup keyword the compiler doesn’t complain with what so ever name you call the subscript method and at the same time it is type safe.
Let try this in playground.
@dynamicMemberLookup
struct Person
{
subscript(dynamicMember name:String) -> Int
{
return name.count
}
}
let personObj = Person()
personObj.pqwrs
//Prints
5
So it’s No matter what’s the name of the property it call the subscript method name
// 1
@dynamicMemberLookup
class Employee {
let name: String
let age: Int
private let empPersonal: [String: String]
init(name: String, age: Int, details: [String: String]) {
self.name = name
self.age = age
self.details = details
}
// 2
subscript(dynamicMember key: String) -> String {
switch key {
case “info”:
return “\(name) is \(age) years old.”
default:
return details[key] ?? “”
}
}
}
// Creating Object of the Class
let details = [“department”: “IT”, “location”: “Mumbai”]
let me = Employee(name: “Logan”, age: 32, details: details)
me.info // “Logan is 32 years old.”
me.department // “IT”[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column][/vc_column][/vc_row]
Author
-
With 14+ years in IT and entrepreneurship, I co-founded Nimap Infotech, a digital transformation company that has delivered 1200+ projects and built a team of 400+ engineers. I’ve also led mobile development teams at Accenture India and IBM Apple Garage and developed a network of 7k+ iOS and Android developers. As an Angel Investor, tech advisor, and mentor, I actively engage with the startup ecosystem.
View all posts