26. What is the meaning of a GUARD statement? What are the
advantages of using Swi 's GUARD statement?
When one or more conditions are not met, a GUARD statement is used to transfer
program control out of the scope. This remark aids in avoiding the doomsday
pyramid. The following is the syntax of a GUARD statement:
guard condition else
{
Statements
}
27. What do you understand about generics in ios Swi ?
Generics are a way to avoid code duplication. It is usual to repeat a method that takes
one type of parameter to accommodate a parameter of a different type. Generics can
be used in both functions and data types in Swi , such as classes, structures, and
enumerations.
func integerEquality(_ a: Int, _ b: Int) -> Bool {
return a == b
}
func stringEquality(_ a: String, _ b: String) -> Bool {
return a == b
}
stringEquality("hello", "hello") // returns true
integerEquality(5, 5) // returns true
For example, the second function in the above code is a "clone" of the first, but it
accepts texts rather than numbers.
Page 24
© Copyright by Interviewbit
Swi Interview Questions
func commonEquality (_ a: T, _ b: T) -> Bool {
return a == b
}
commonEquality("hello", "hello") // returns true
commonEquality(5, 5) // returns true
You can consolidate the two functions into one and maintain type safety at the same
time by using generics. Given above is the way to do it in general.
Because you are testing equality here, you can use any type that implements the
Equatable protocol as a parameter. This code achieves the desired outcome while
preventing the use of non-typed parameters.
28. What do you understand about optionals in ios Swi ? What
Do'stlaringiz bilan baham: |