5 ms·
Does it have an equivalent to Swifts if let clause? if let person = selection?.organization?.owner { setTitle(person.name) setImage(person.
by seanalltogether 9y ago
Does it have an equivalent to Swifts if let clause?
if let person = selection?.organization?.owner {
setTitle(person.name)
setImage(person.image)
}
- asadmshah 9y agoYes. val person = selection?.organization?.owner?.let { setTitle(it.name) setImage(it.image) it }
- metroidfan832 9y agoYou can use `also` instead of `let` to remove the last `it` line.
- amatheus 9y agoYou may use `also`. It is like let but it returns the receiver. val person = selection?.organization?.owner?.also { setTitle(it.name) setImage(it.image) }
- seanalltogether 9y agook so I'm guessing you can't operate on more then one scope at once? if let person1 = dashboard?.owner, person2 = selection?.organization?.owner { person1.cloneProperties(person2) }
- kodablah 9y agoYou'd use nested let clauses and name the param instead of using the implicit "it"
- meddlepal 9y agoOr just write a function to accomplish the same behavior: http://stackoverflow.com/a/35522422/364135 http://stackoverflow.com/a/35522422/364135 Would be nice if there was a multiLet in the stdlib. Probably something that happens eventually anyways.
- ionforce 9y agoIs this setting a value to the name `person`? What is the value?
- heifer2822 9y agoselection?.organization?.owner is an optional value, meaning it might be nil/null. That "if let" basically says if it's not nil to assign its value to "person" and execute the block. Then inside the block you can treat "person" as a non-optional, known value.