官术网_书友最值得收藏!

Initialization

The process of preparing an instance of a class, structure, or enumeration for use is called initialization. Classes and structures must set all of their stored properties to an appropriate initial value by the time an instance of that class or structure is created. Stored properties cannot be left in an intermediate state. We can modify the value of a constant property at any point during initialization as long as it is set to a definite value by the time initialization finishes. Swift provides a default initializer for any structure or base class that provides default values for all of its properties and does not provide at least one initializer itself. Consider the following example:

class ShoppingItem { 
var name: String?
var quantity = 1
var purchased = false
}
var item = ShoppingItem()

The struct types automatically receive a member-wise initializer if we do not define any of our own custom initializers, even if the struct's stored properties do not have default values.

Swift defines two kinds of initializers for class types:

  • Designated initializers: Methods that are able to fully initialize the object
  • Convenience initializers: Methods that rely on other methods to complete initialization
主站蜘蛛池模板: 和林格尔县| 祁东县| 突泉县| 杂多县| 商水县| 秦皇岛市| 临洮县| 江都市| 塔城市| 桃源县| 印江| 兰溪市| 邛崃市| 阿合奇县| 乳源| 乌海市| 镇宁| 河津市| 沁阳市| 清丰县| 巫溪县| 临沧市| 丹棱县| 叙永县| 天峨县| 叙永县| 长岛县| 武义县| 阿瓦提县| 施甸县| 余庆县| 大名县| 靖安县| 甘孜| 济源市| 舒城县| 营口市| 西昌市| 马山县| 青阳县| 高清|