Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to do unwrapping issue inside If-let statement?

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)
    }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading