Home > Net >  How to do unwrapping issue inside If-let statement?
How to do unwrapping issue inside If-let statement?

Time:09-23

I am using if let for getting the object if its not nil. But I also need to check other condition as well i.e., if "treatmentContext.patientTreatment.canWritePermissions.contains(treatmentContext.pathPatientTreatment.owner". That I am able to do by putting comma after the first statement but here the issue is, I need to unwrap the value of treatmentContext.pathPatientTreatment.owner and here I don't know where exactly I need to unwrap that so that my if condition gets pass when it meets all the criteria. Below is the code for reference.

if let treatmentContext = IoC.resolve(Treatment.self, from: .treatment), treatmentContext.patientTreatment.canWritePermissions.contains(treatmentContext.pathPatientTreatment.owner) 
     {
    self.presentNavigation(isNew: isNew)
    }

CodePudding user response:

You already know you can separate the conditions with ,, so just do that again, but this time it's another optional binding clause:

if let treatmentContext = IoC.resolve(Treatment.self, from: .treatment), 
   let owner = treatmentContext.pathPatientTreatment.owner,
   treatmentContext.patientTreatment.canWritePermissions.contains(owner) {
    self.presentNavigation(isNew: isNew)
}

You can separate any number of optional binding clauses, Bool conditions, or case ... patterns with , in an if.

  • Related