Pregunta de entrevista de Allstate

For Swift, describe optional binding vs optional chaining

Respuesta de la entrevista

Anónimo

10 abr 2017

Another question I froze up on (until the architect said “if nil?”), where I immediately realized he was talking about this very fundamental Swift language feature. Optionals in Swift mean that the object can be a valid object or it can be nil. And if I have an array of strings of fruit names and I try to find the index of “hamburger” in that array, I should be able to handle a nil result (i.e. this is optional binding). Optional chaining refers to when a chain of values could have optionals in it. Such as school.director?.name (where director would indicate an optional).