Google News
logo
Swift Interview Questions
Automatic Reference Counting is used by Swift (ARC) in order to do memory management. This is the same thing in Swift as it is in Objective C in terms of notion. When you assign or unassign instances of classes (reference types) to constants, properties, and variables, ARC keeps track of strong references to those instances and increases or decreases their reference count correspondingly. It frees up memory consumed by objects with a reference count of zero. Because value types are copied when assigned, ARC does not raise or decrease its reference count. If you don't declare differently, all references will be strong references by default.
The key difference between Upcast and Downcast in ios Swift is that upcasting from a derived to a base class can be verified at compile-time and will never fail to compile and Downcasts, on the other hand, can fail to compile since the precise class is not always known. It is possible that the UIView you have is a UITableView or a UIButton.
The fundamental difference between the "==" operator and the "===" operator in ios Swift is that the equal to "==" operator compares value types to see if the values are the same while the equivalent to "===" operator compares reference types to see if the references point to the same instance (both point to the same memory address) or not.  Let us consider the following example for understanding the difference between the two in a better way :
class Human: Equatable {
   let id: Int
   let nameOfPerson: String
   init(id: Int, nameOfPerson: String) {
       self.id = id
       self.nameOfPerson = nameOfPerson
   }
   static func == (left: Human, right: Human) -> Bool {
       return left.id == right.id
   }
}
let human1 = Human(id: 2, nameOfPerson: "Janvi")
let human2 = Human(id: 2, nameOfPerson: "Janvi")
 
Now, for the piece of code given below, we can say that the two human instances are equal since their id is the same. Therefore, "Equal Instances!" gets printed.
if human1 == human2 {
  print("Equal Instances!")
}else{
  print("Instances Not Equal!")
}
 
Now, for the piece of code given below, we can say that the two human instances are not equivalent even though their id is the same since they point to different areas in the Heap Area, that is, they point to different addresses. Therefore, "Instances are not Equivalent!" gets printed.
if human1 === human2 {
  print("Equivalent Instances!")
}else{
  print("Instances are not Equivalent!")
}
A function inside a function is called a nested function.
 
Syntax :
func function1() {    
    //statements of outer function    
    func function2() {    
        //statements of inner function    
    }    
}
Enum is also known as Swift Enumeration. Enum is a data type which contains a set of the related values. It is declared in a class and its values are accessed through the instance members of that class.
 
Syntax :
enum enum_name    
{    
// values are described here    
}
Regular Expression : Regular expressions are the special string patterns that specify how to perform a search through a string.
 
Responder Chain : Responder Chain is a hierarchy of objects that obtain the opportunity to respond to the events.
Swift Dictionary is used to store the key-value pairs and access the value by using the key. It is just like hash tables in other programming languages.
You have to declare variables and constants before using it. Constants are declared by using let keyword and variables by var keyword.
 
Example :
var freetimelearn = "Free Time Learning"  
let fees = 100 
It is used to provide the default value for the variable.
 
let missingN1 : String? = nil
 
let realN1 : String = “Vikas”
 
let existentN1 : String = missingN1 ?? realN1
We can create constant in Swift programming by declaring a ‘let’ keyword. Below is the example where we use declare street as a constant in lieu of a variable. For this, we have to use let keyword :
let street: String = “7th Avenue”
var number: Int
street = “Side Street”
number = 10

 

We cannot change the value of constant in Swift. If we try to update the first line of the code, the Xcode will show an error for a simple reason. To change the value of the street, we can comment on the given line, so that it will not create any type of error.
let street: String = “7th Avenue”
var number: Int
// street = “Side Street”
number = 10