func swapTwoValues<T>(_ a: inout T, _ b: inout T) { let temporaryA = a a = b b = temporaryA } var num1 = 4 var num2 = 5 var str1 = βaβ var str2 = βbβ swapTwoValues(&num1,&num2) swapTwoValues(&str1,&str2) print (βnum1:β, num1) //output: 5 print (βnum2:β, num2) //output: 4 print (βstr1:β, str1) //output: b print (βstr2:β, str2) //output: a
let value: Int? = 1 let newValue: Int = value! // newValue 1 let anotherOptionalInt: Int? = nil let anotherInt = anotherOptionalInt! // Output:fatal error: nil .
var name: String! = βViratβ let student = name // name = nil let player = name //Output:fatal error: nil .
let possibleString: String? = "Hello" if let actualString = possibleString { //actualString - ( ) // , possibleString print(actualString) } else { //possibleString , // }
Guard
and what are its advantages?guard
is simple and powerful. It checks a condition and, if it is evaluated as false, the else statement is executed, which usually terminates the method.
func addStudent(student: [String: String]) { guard let name = student["name"] else { return } print("Added \(name)!") guard let rollNo = student ["rollNo"] else { print("roll No not assigned") return } print("Assigned roll no is \(rollNo).") } addStudent(student: ["name": "Ravi"]) // "Added Ravi!" // "roll No not assigned" addStudent(student: ["name": "Ravi", "rollNo": "1"]) // "Added Ravi!" // "Assigned roll no is 1"
guard
is faster execution . Guard
block is executed only if the condition is false, and the block is exited through a control transfer operator, such as return
, break
, continue
or thrown
. This provides an early exit and fewer brackets. Early exit means faster execution.
guard let
, and when if let
?
guard
when you want to eliminate unexpected / incorrect input and focus on the target if you have alternative ways of handling input.guard
if
else
block to reduce nesting and indentation, as it is relatively compact.defer
?defer
operator defer
used to execute a set of statements immediately before the execution of the code leaves the current block.
defer
inside the if
block will be executed first. Then follows the LIFO template to execute the remaining defer
statements.
func doSomething() { defer { print(β1β)} defer { print(β2β)} defer { print(β3β)} if 1<2 { defer { print("1<2")} } defer { print(β4β)} defer { print(β5β)} defer { print(β6β)} } 1<2 6 5 4 3 2 1
break
- The break
statement immediately terminates the entire control flow statement.
continue
- the continue
statement tells the loop to stop what it is doing, and to start over at the beginning of the next iteration of the loop.
return
- returns values ββfrom functions.
throw
- need probros errors using Throwing Functions
fallthrough
- The fallthrough
operator fallthrough
used in a switch case block to execute a case statement that is next to the corresponding case statements based on user requirements.
swift
the fallthrough
operator fallthrough
used to execute the next case, even if it does not match the original one.
let integerToDescribe = 5 var description = βThe number \(integerToDescribe) isβ switch integerToDescribe { case 2, 3, 5, 7, 11, 13, 17, 19: description += β a prime number, and alsoβ fallthrough case 10: description += β case 10.β default: description += β an integer.β } print(description)// :The number 5 is a prime number, and also case 10.
Source: https://habr.com/ru/post/452806/