Search Your Question

Difference Between Inheritance And Extensions?

Ans : 

When to use extension : 
Are you adding general-purpose functionalities that should be available to every UITextField? If so, make an extension. All UITextField instances can call the new methods.

When to use inheritance : 
Are you adding functionality that should be restricted to special instances of UITextField that you would identify precisely? If so, make a subclass. Only the instances of the subclass can use the new methods.

Another diff :

In extension, there is no any specific name of extension in swift, but while subclassing there is another name of subclass.

In extension, we can not add variables (fields), in subclass it is possible to declare variables.


How To Access AppDelegate Methods In Other Class?

Ans : 

let appDelegate: AppDelegate? = UIApplication.shared.delegate as? AppDelegate
appDelegate?.application(UIApplication.shared, didFinishLaunchingWithOptions: nil)

You can make your custom method any called from viewcontroller like :

let appDelegate = UIApplication.shared.delegate as! AppDelegate appDelegate.anyAppDelegateInstaceMethod()

But above is only possible in iOS 10.0 or newer version.

Difference Between If Let And Guard Let?

Ans : 

Basic Difference :

Guard let 

Early exist process from the scope
Require score existing like return, Throw etc.
Create a new variable those can be access out the scope.

if let 

Can not access out the scope.
no need to return statement. But we can write

Note : Both are used to unwrapped the Optional variable.


Guard let


  • A guard statement is used to transfer program control out of a scope if one or more conditions aren’t met. 
  • The value of any condition in a guard statement must be of type Bool or a type bridged to Bool. The condition can also be an optional binding declaration.


guard condition else { //Generally return }
func submit() {
guard let name = nameField.text else {
    show("No name to submit")
    return

}

If let
  • Also popular as optional binding 
  • For accessing optional object we use if let
if let roomCount = optionalValue {
        print("roomCount available")
} else {
       print("roomCount is nil")

}



Q. return is mandatory in guard let statement ?
A. Exit is mandatory in guard let statement. So return or throw is mandatory in guard let. Otherwise it gives compile time error.


Difference Between UIWindow And UIView?

Ans :

Windows do not have any visible content themselves but provide a basic container for your application’s views. 

Views define a portion of a window that you want to fill with some content. 

Note : Typically, there is only one window in an iOS application.



What Is IBInspectable And IBDesignable?

Ans : 

IBDesignable and IBInspectable , a way to create custom elements and the attributes . This can be directly added to the iOS Interface Builder.


IBDesignable : 

IBDesignable attribute will identify the UIView or the elements inherited from UIView
i.e: UIButton, UIImageView, UILabel etc

Code :

@IBDesignable
open class KGHighLightedButton: UIButton {

}

IBInspectable : 

@IBInspectable var borderWidth: Double {
        get {
                 return Double(self.layer.borderWidth)
              }
       set {
                 self.layer.borderWidth = CGFloat(newValue)
            }
  }

@IBInspectable var borderColor: UIColor?  {
       get {
                return UIColor(cgColor: self.layer.borderColor!)
             }
      set {
               self.layer.borderColor = newValue?.cgColor
           }
  }

Due to above code, you can set above properties in attribute inspector like following :


So using @IBDesignable and @IBInspectable, you can change make inspectable property and see live changes in IB interface builder without run.

Note : 

IBInspectable can be used with the below types,

Int
CGFloat
Double
String
Bool
CGPoint
CGSize
CGRect
UIColor
UIImage





Wrapping and UnWrapping in Swift

Ans : 

An Optional is basically means that the variable can be nil

i.e: 

var itCanBeNil: String? = “You can make me nil”
itCanBeNil = nil
Question Mark(?) indicates the fact that itCanBeNil Variable can be nil

Let’s try  to make nil a variable which declared without Optional ?

i.e

var itCantBeNil: String = “You can’t make me nil”
itCantBeNil = nil  //Compiler Throws Exception as “Nil cannot be assigned to type ‘String’ ‘”

If we need to get the value from the Variable if it is Optional, we need to unwrap it,
unwrapping is nothing but just putting an exclamation mark at the end

i.e

var itCanBeNil: String? = “Unwrap me”
print(itCanBeNil) //Output: Optional(Unwrap me)
print(itCanBeNil!) //Output: Unwrap me

We can also declare optionals to automatically unwrap by using exclamation mark instead of a question mark

i.e

var itCanBeNil: String! = “Automatic unwrap”

print(itCanBeNil) //No Wrapping needed

What method is called after tap back button in ios?

Ans : 

When you go back the following methods will be called:

Notifies the view controller that its view is about to be added to a view hierarchy : 

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
}


Notifies the view controller that its view was added to a view hierarchy : 

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

}

What is stored property and computed property?

Ans : 

1. Stored Property : 

A stored property is a constant or variable that is stored as part of an instance of a particular class or structure. Stored properties can be either variable stored properties (introduced by the var keyword) or constant stored properties (introduced by the let keyword).

struct FixedLengthRange {
    var firstValue: Int
    let length: Int
}

So here, firstValue is mutable (var) storedProperty that can have value and change value,
length is immutable (let) computedProperty that can have value but can not be changed.

We can add property observers to any stored properties you define, except for lazy stored properties.

You have the option to define either or both of these observers on a property:
willSet is called just before the value is stored. (newTotalSteps)
didSet is called immediately after the new value is stored. (oldValue)

2. Computed Property :

Classes, structures, and enumerations can define computed properties, which do not actually store a value. Instead, they provide a getter and an optional setter to retrieve and set other properties and values indirectly.

struct Point {
    var x = 0.0, y = 0.0
}
struct Size {
    var width = 0.0, height = 0.0
}
struct Rect {
    var origin = Point()
    var size = Size()
    var center: Point {
        get {
            let centerX = origin.x + (size.width / 2)
            let centerY = origin.y + (size.height / 2)
            return Point(x: centerX, y: centerY)
        }
        set(newCenter) {
            origin.x = newCenter.x - (size.width / 2)
            origin.y = newCenter.y - (size.height / 2)
        }
    }
}

What is priority in constraints?

Ans : 

Constant priority is a number to determine how important is that constraint. The number can range from 1 to 1000, the higher the number goes, the more important a constraint is. Lower priority in screen seen like dashed blue line.

This is useful when two constraint make conflict.

Example :

Let's take one UIView and place them in main view.

Set Leading and Trailing constraint is 40,40.
Set width and height is 240,240.

Now if iPhone SE is selected, then all goes fine. No red line will show about conflicting constraints due to iPhone SE width is 320. So if it takes 40,40 as leading and constraints, it will calculate view's width as 240. And we also set width constraint as 240. So there is no conflicting.

But if iPhone 8 is selected then view's width constraint is conflicting with leading, trailing constraints.
iPhone 8 width = 375 - 40 - 40 = 295 But we have set width constraint as 240. So here conflicting occurs.

Solution : 

If we set width constraint priority 900 then its priority is lower than leading and trailing priority(1000) So width constraint will be ignored here. So when we do some auto layout and we feel conflicting constraint, we can lower priority of constraint according to requirement.


Device token will be changed or not if we uninstall and reinstall the app ?

Ans : 

The Device token is change in the following conditions.

  • If the user restores backup data to a new device.
  • Reinstalls the App
So update to server from app delegate method didRegisterForRemoteNotificationsWithDeviceToken every time. (Just suggestion)