Search Your Question

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