- Swift Functional Programming(Second Edition)
- Dr. Fatih Nayebi
- 196字
- 2021-07-02 23:54:29
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
- 數(shù)據(jù)庫(kù)基礎(chǔ)教程(SQL Server平臺(tái))
- 數(shù)據(jù)浪潮
- Building Computer Vision Projects with OpenCV 4 and C++
- MySQL從入門到精通(第3版)
- Mastering Machine Learning with R(Second Edition)
- 智能數(shù)據(jù)分析:入門、實(shí)戰(zhàn)與平臺(tái)構(gòu)建
- 編寫有效用例
- 計(jì)算機(jī)組裝與維護(hù)(微課版)
- SQL Server 2012實(shí)施與管理實(shí)戰(zhàn)指南
- 從Lucene到Elasticsearch:全文檢索實(shí)戰(zhàn)
- Unity Game Development Blueprints
- AndEngine for Android Game Development Cookbook
- 數(shù)據(jù)挖掘算法實(shí)踐與案例詳解
- 數(shù)據(jù)中心UPS系統(tǒng)運(yùn)維
- Configuration Management with Chef-Solo