In Swift, what's the difference between calling UINavigationController() vs UINavigationController.init()?

后端 未结 3 1932
时光取名叫无心
时光取名叫无心 2020-12-04 00:46

In Swift, what\'s the difference between calling UINavigation() vs UINavigation.init()? They both seem to return valid instance of UINavigati

3条回答
  •  半阙折子戏
    2020-12-04 01:26

    For some given type (e.g. UINavigationController), there is no difference between a call to UINavigationController() or UINavigationController.init(), but the latter syntax can (without the () call) be useful when referencing an initializer of some given type, say Foo, in contexts where we want to make use of a closure (or reference to a closure) which is to have

    • zero or more arguments, and
    • return type Foo,

    e.g., (Int, Double) -> Foo. In these contexts, using the syntax Foo.init may prove useful: rather than explicitly letting a closure repeatedly call a known initializer (piping the closure's arguments to the initializer), we may use (a reference to) the initializer direcly as the closure instead. If there's no ambiguity in the argument of the initializers of Foo, a reference to Foo.init in some given closure type context will resolve, using type inference, to the correct initializer.

    E.g., consider the following example

    struct Foo {
        let foo: Int
    
        // (Int) -> Foo
        init(foo: Int) {
            self.foo = 2*foo
        }
    
        // (Int, Int) -> Foo
        init(foo: Int, bar: Int) {
            self.foo = foo + bar
        }
    
        // () -> Foo
        init() {
            self.foo = 42
        }
    }
    
    let arr = [1, 2, 3]
    let fooArr1 = arr.map { Foo(foo: $0) }
    let fooArr2 = arr.map(Foo.init)
                     /* map operation expects a single argument of type (Int) -> Foo, 
                        which we generally supply as a trailing closure. In this context,
                        Swift can, without ambiguity (since we have none, is this example), 
                        find the correct overload among the initializers of Foo */
    print(fooArr1.map { $0.foo }, fooArr2.map { $0.foo }) // [2, 4, 6] [2, 4, 6]
    
    let emptyTupArr = [(), (), ()]
    let fooArr3 = emptyTupArr.map(Foo.init) // inferred to be the '() -> Foo' initializer
    print(fooArr3.map { $0.foo }) // [42, 42, 42]
    

提交回复
热议问题