Simple syntax for Swift's entry (v)

Source: Internet
Author: User

Object-oriented targets
    • constructor function
      • Basic concepts of constructors
      • Execution order of constructors
      • KVCThe use and principle of the structure function
      • Convenient constructor function
      • Destructors
      • Distinguishing 重载 and重写
    • Lazy Loading
    • Read-only properties (computed properties)
    • Set model data ( didSet )
Constructor Basics

构造函数is a special function that is used primarily to initialize an object when it is created, to set an initial value for an object, the 成员变量 constructor in OC is Initwithxxx, and in Swift, because of the support for function overloading , all constructors areinit

function of the constructor

    • Allocate spacealloc
    • Set the initial valueinit
Required Properties
    • Custom Person Objects
class Person: NSObject {    /// 姓名 var name: String /// 年龄 var age: Int}

Prompt Error- Class ‘Person‘ has no initializers‘Person‘ 类没有实例化器s

Cause: If a required attribute is defined in a class, you must allocate space for these required properties through the constructor and set the initial value

    • 重写The constructor of the parent class
/// `重写`父类的构造函数override init() {}

Prompt Error- Property ‘self.name‘ not initialized at implicitly generated super.init call属性 ‘self.name‘ 没有在隐式生成的 super.init 调用前被初始化

    • Adding super.init() calls Manually
/// `重写`父类的构造函数override init() {    super.init()}

Prompt Error- Property ‘self.name‘ not initialized at super.init call属性 ‘self.name‘ 没有在 super.init 调用前被初始化

    • Set the initial value for the required property
/// `重写`父类的构造函数override init() {    name = "张三"    age = 18 super.init()}
Summary
    • Non-Optional property, you must set the initial value in the constructor to ensure that the property is correctly initialized when the object is instantiated.
    • Before calling the parent class constructor, you must ensure that the properties of this class have been initialized
    • The constructor in Swift does not have to be writtenfunc
Constructors for subclasses
    • When customizing a subclass, you need to set the initial values for the properties defined in this class first in the constructor
    • Then call the parent class's constructor to initialize the properties defined in the parent class.
/// 学生类class Student: Person { /// 学号 var no: String override init() { no = "001" super.init() }}
Summary
    • The constructors of this class are called first to initialize the properties of this class
    • Then call the parent class's constructor to initialize the properties of the parent class
    • After Xcode 7 Beta 5, the constructor of the parent class is automatically called, strongly recommended super.init() , and the readability of the code execution thread is maintained
    • super.init()Must be placed after the initialization of this class of properties to ensure all initialization of this class of properties is complete
OptionalProperty
    • Set the object property type toOptional
class Person: NSObject {    /// 姓名 var name: String? /// 年龄 var age: Int?}
    • 可选属性Does not need to set the initial value, the default initial value is nil
    • 可选属性Allocates space when setting values, is deferred allocation space, more in line with the principle of delayed creation in mobile development
Overloaded constructors
    • Support for function overloading in Swift, same function name, different parameter types
/// `重载`构造函数////// - parameter name: 姓名/// - parameter age:  年龄////// - returns: Person 对象init(name: String, age: Int) { self.name = name self.age = age super.init()}
Precautions
    • If the constructor is overloaded, but the default constructor is not implemented init() , the system no longer provides a default constructor
    • Cause, when instantiating an object, you must allocate space and set an initial value through the constructor for the object property, and for the class that has the required parameters, the default init() cannot complete the allocation space and set the initial value of the work
To adjust the constructor of a subclass
    • 重写The constructor of the parent class
/// `重写`父类构造函数////// - parameter name: 姓名/// - parameter age:  年龄////// - returns: Student 对象override init(name: String, age: Int) { no = "002" super.init(name: name, age: age)}
    • 重载constructor function
/// `重载`构造函数////// - parameter name: 姓名/// - parameter age:  年龄/// - parameter no: 学号////// - returns: Student 对象init(name: String, age: Int, no: String) { self.no = no super.init(name: name, age: age)}

Note: If you are overloading a constructor, you must super work with the initialization of the parent class property to complete

重载And 重写
    • 重载, function name is the same, parameter name/parameter type/parameter number is different
      • Overloaded functions are not just limited to构造函数
      • Function overloading is an important symbol of face object programming language
      • Function overloading simplifies programmer's memory
      • OC does not support function overloading, which is an alternative to OCwithXXX...
    • 重写, subclasses need to be extended on the basis of the parent class's own methods, requiring the override keyword

Simple syntax for Swift's entry (v)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.