class Person { var firstName: String? var middleName: String? var lastName: String? var fullName: String { get { return ("\(firstName) \(middleName) \(lastName)") } } init (firstName: String, middleName: String, lastName: String) { self.firstName = firstName self.middleName = middleName self.lastName = lastName } } var person1 = Person(firstName: "23123 ", middleName: " 123123", lastName: "123123 ") 

when we call person1.fullName, we receive Optional ("23123") Optional ("123123") Optional ("123123") need not optional how to do it correctly in the class

    2 answers 2

    To get rid of optional, you need to force unwrap by adding '!'. Remember that your application will crash if it stumbles on nil, that is, if you call firstName! at the time when firstName == nil , then it is crash.

     self.firstName = firstName! 

    At the same time, I would say that such a decision is wrong, but it would be right to get rid of the "?" in the declaration of the instance variables, since you assign them all to the internet.

    • is there such a moment that firstName may not come to us, and in this case, var firstName: String? should stay - DmitrievichR
    • How to make in class a test of the type if firstName! = nil {self.firstName = firstName! } Can I do this in classes? Please help me;) - DmitrievichR
    • you can do that, the code in your comment is absolutely correct. I would recommend if some string came nil, replace with an empty string - "", so you will know for sure that you have nowhere nil cannot meet. again, remove the '?' - Max Mikheyenko pm
    • I did just add init to all options. Thank! - DmitrievichR
    • one
      @Igor depends on the situation. if for example a person does not have a middle name, it is easier to put "" and not to check for nil in a million places in the application. - Max Mikheyenko pm

    There are two good options to expand (get non-optional) variable:

    The first:

     if let firstName = firstName { // можно задать другое имя, например firslNameNotOptional = firstName print("уже развернута - не опциональная") print(firstName) } else { print("nil") print(firstName) } //после блока if firstName остается опциональной! 

    You can immediately wholesale:

     if let firstName = firstName, middleName = middleName, lastName = lastName { //здесь получим уже не опциональные firstName, middleName, lastName //но только при условии, что удалось развернуть все три сразу } 

    One important point is that we only get the expanded value inside an if {} block if {}

    Second:

     guard let firstName = firstName else { print("firstName - nil") return } print("уже развернута - не опциональная") print(firstName) 

    in the whole code until the very end of the method after guard {} we get the expanded firstName

    • Thank you very much for the answer! - DmitrievichR
    • Isn't that what was needed? - Igor
    • I blunted enough to initialize = "" as an empty string. Your answer was also correct, but I used another option - DmitrievichR