search

Home  >  Q&A  >  body text

ios - 关于swift中的默认构造器

文档说如果结构体或类的所有属性都有默认值,同时没有自定义的构造器,就会有默认构造器。
默认构造器是不是这样,但是不必显式声明。

init() {
    //
}

而且默认构造器属不属于指定构造器?文档说每个类都至少有一个指定构造器。

PHP中文网PHP中文网2887 days ago465

reply all(1)I'll reply

  • 巴扎黑

    巴扎黑2017-04-17 17:33:30

    You don’t need to think about too complicated theories and definitions. Just create a new playground and experience the following code to understand.

    struct Structure {
      var string = ""
      var number = 0
    }
    
    let sA = Structure()
    let sB = Structure(string: "hello", number: 100)
    
    print(sA)
    print(sB)
    
    class Class {
      var string = ""
      var number = 0
    }
    
    let cA = Class()
    print(cA)
    // let cB = Class(string ....) // 不可以
    

    reply
    0
  • Cancelreply