0

I think I understand how optionals work in Swift, but sometimes I fail to see the logic, and I can't derive it from the docs either, so I hope someone can help me here.

The issue is this: I have a class declared as follows:

class Person {
    let firstName:String!
    let lastName:String!

    init(firstName:String!,lastName:String!) {
        self.firstName = firstName
        self.lastName = lastName
    }
}

Next I instantiate the class and use it (code can be simplified but I just pulled what my app is doing without showing all the boilerplate around it, so please don't suggest me how to optimize this):

let myPerson = Person.init("First","Last")
var aSecondPerson:Person!

...
aSecondPerson = myPerson

let someString = "\(aSecondPerson.firstName) \(aSecondPerson.lastName)"

Now I would assume the contents of someString to be First Last but instead it is: Optional("First") Optional("Last")

Why is it still considering the values as optional considering the amount of ! used all over the place?

Joris Mans
  • 6,024
  • 6
  • 42
  • 69

0 Answers0