Showing posts with label multiple. Show all posts
Showing posts with label multiple. Show all posts

Wednesday, September 26, 2018

Swift - Multiple levels of Optional Binding


If there is Optional, We tend to bind them using guard or if let multiple levels which should be based on what we need exactly.

     
  class Person {

    var name: String?
    weak var spouse: Person?
    
      init(name: String) {
          self.name = name
      }

   }

    let wife = Person(name: "Wife")
    let husband = Person(name: "Husband")
    wife.spouse = husband
    husband.spouse = wife

  

I need to display spouse name in a label.

We don’t need two bindings like below.

     
   if let spouse = husband.spouse, 
                   let name = spouse.name {
        lblSpouseName.text = name
   }

  
Cause, We are not using binded spouse(let spouse = husband.spouse) object. We are using only minded spouse name(let name = spouse.name)

As here what we need is only spouse name, We can take help of optional chaining in optional binding.

     
  if let name = husband.spouse?.name {
      lblSpouseName.text = name
  }

  

husband.spouse?.name is optional chaining and gives value only If husband’s spouse is not nil and spouse name is also not nil.

That’s how we need to use binding for the things we need in the scope of If let or guard let.

In the above case, Coalescing operator works like charm and best fit where we can give a default value as well.


     
  lblSpouseName.text = husband.spouse?.name ?? "NA"
    
  

Hope this post is useful. Feel free to comment incase of any queries.



Wednesday, April 18, 2018

If-Let & Guard-Let Multiple Optional Bindings


Instead of nesting with multiple optional binding statements using if-let or guard-let, We can use single statement for multiple optional bindings.

Let’s take a sample Employee class which is having both first name and last name as optionals.

If we need to get full name of employee, Let’s see how can we get it using nested and single optional binding statements.

-------------------------------------------------------------------
class Employee {    
    var firstName: String?
    var lastName: String?
    
    var empID: String
    
    init(employeeID: String) {
        self.empID = employeeID
    }
    
}

-------------------------------------------------------------------


let emp = Employee(employeeID: "123")
emp.firstName = "Karunakar"
emp.lastName = "Bandikatla"

var fullName = ""

-------------------------------------------------------------------

#Nested

-------------------------------------------------------------------
if let firstName = emp.firstName {

    if let lastName = emp.lastName {
        fullName = firstName + " " + lastName
    }
}
-------------------------------------------------------------------


#Single If-Let

-------------------------------------------------------------------

if let firstName = emp.firstName, 
   let lastName =  emp.lastName {

    fullName = firstName + " " + lastName
}

-------------------------------------------------------------------


Same way we can write guard-let.

-------------------------------------------------------------------
func getFullName(emp: Employee) -> String {
    
    guard let firstName = emp.firstName, 
             let lastName = emp.lastName else{
        return ""
    }
    
    return firstName + " " + lastName
    
}

fullName = getFullName(emp: emp)
-------------------------------------------------------------------

Hope this post is useful. Feel free to comment incase of any queries.