Implicitly unwrapped optional from init!() in Swift 3.1

Robo Robok

I'm trying to understand how init!() works in Swift. Here's my test:

struct InitTest {
    var text: String
    init!(text: String) {
        self.text = text
    }
}

let testResult = InitTest(text: "Hello!")

For my understanding, testResult should be of type InitTest (unwrapped), but it's actually still InitTest?.

How is init!() different from init?() then?

Martin R

InitTest(text: "Hello!") returns an implicitly unwrapped optional, which is an optional that is unwrapped if necessary. For example you can access its properties without explicit unwrapping

let string = InitTest(text: "Hello!").text

or pass it to functions taking a (non-optional) InitTest argument:

func foo(_ x: InitTest) { }
foo(InitTest(text: "Hello"))

But the assignment

let testResult = InitTest(text: "Hello!")

makes testResult a regular ("strong") optional, see SE-0054 Abolish ImplicitlyUnwrappedOptional type and Implicitly unwrapped optional assign in Xcode 8:

If the expression can be explicitly type checked with a strong optional type, it will be.

Actually I cannot think of a good reason to define an init!() method.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Swift 3 incorrect string interpolation with implicitly unwrapped Optionals

From Dev

In Apple Swift, in what case(s) would I not want an implicitly unwrapped optional?

From Dev

Implicitly Unwrapped Optionals in UIViewController init method

From Dev

Implicitly unwrapped optional made immutable

From Dev

Optional Binding on Implicitly Unwrapped Optional

From Dev

Implicitly unwrapped optional in Apple methods

From Dev

Implicitly Unwrapped Optionals in Initialization - Swift

From Dev

Accessing valueless implicitly unwrapped optional?

From Dev

Returning an implicitly unwrapped optional

From Dev

Implicitly Unwrapped Optional Types - Possible Typo?

From Dev

Why is a Swift implicitly unwrapped optional `nil`?

From Dev

Implicitly Unwrapped Optionals in Swift does not seem to work

From Dev

Swift's use of Implicitly Unwrapped Optional before availability of nullability

From Dev

Swift unary operator with implicitly unwrapped optional

From Dev

Implicitly unwrapped optional Closure in a method argument

From Dev

Swift: Comparing Implicitly Unwrapped Optionals results in "unexpectedly found nil while unwrapping an Optional values"

From Dev

Incrementing an implicitly unwrapped optional

From Dev

How to get the unwrapped type from an optional type in Swift?

From Dev

What is the point of using implicitly unwrapped optional in this example?

From Dev

Implicitly Unwrapped Optionals in UIViewController init method

From Dev

Implicitly unwrapped optional in Apple methods

From Dev

Implicitly Unwrapped Optionals in Initialization - Swift

From Dev

Returning an implicitly unwrapped optional

From Dev

Generics not working with implicitly unwrapped optional

From Dev

Swift Optional Unwrapped still crashes

From Dev

Implicitly unwrapped optional Closure in a method argument

From Dev

Swift unary operator with implicitly unwrapped optional

From Dev

Swift Optional Type not Unwrapped

From Dev

Swift 3: Meaning of parenthesis around unwrapped optional

Related Related

  1. 1

    Swift 3 incorrect string interpolation with implicitly unwrapped Optionals

  2. 2

    In Apple Swift, in what case(s) would I not want an implicitly unwrapped optional?

  3. 3

    Implicitly Unwrapped Optionals in UIViewController init method

  4. 4

    Implicitly unwrapped optional made immutable

  5. 5

    Optional Binding on Implicitly Unwrapped Optional

  6. 6

    Implicitly unwrapped optional in Apple methods

  7. 7

    Implicitly Unwrapped Optionals in Initialization - Swift

  8. 8

    Accessing valueless implicitly unwrapped optional?

  9. 9

    Returning an implicitly unwrapped optional

  10. 10

    Implicitly Unwrapped Optional Types - Possible Typo?

  11. 11

    Why is a Swift implicitly unwrapped optional `nil`?

  12. 12

    Implicitly Unwrapped Optionals in Swift does not seem to work

  13. 13

    Swift's use of Implicitly Unwrapped Optional before availability of nullability

  14. 14

    Swift unary operator with implicitly unwrapped optional

  15. 15

    Implicitly unwrapped optional Closure in a method argument

  16. 16

    Swift: Comparing Implicitly Unwrapped Optionals results in "unexpectedly found nil while unwrapping an Optional values"

  17. 17

    Incrementing an implicitly unwrapped optional

  18. 18

    How to get the unwrapped type from an optional type in Swift?

  19. 19

    What is the point of using implicitly unwrapped optional in this example?

  20. 20

    Implicitly Unwrapped Optionals in UIViewController init method

  21. 21

    Implicitly unwrapped optional in Apple methods

  22. 22

    Implicitly Unwrapped Optionals in Initialization - Swift

  23. 23

    Returning an implicitly unwrapped optional

  24. 24

    Generics not working with implicitly unwrapped optional

  25. 25

    Swift Optional Unwrapped still crashes

  26. 26

    Implicitly unwrapped optional Closure in a method argument

  27. 27

    Swift unary operator with implicitly unwrapped optional

  28. 28

    Swift Optional Type not Unwrapped

  29. 29

    Swift 3: Meaning of parenthesis around unwrapped optional

HotTag

Archive