Swift Learning note nine: Classes class

Source: Internet
Author: User

    • constructor function
      • Non-Optional property, you must set the initial value in the constructor to ensure that the properties are initialized correctly when the object is instantiated
      • Purpose of the constructor: Assign space to your own property and set the initial value
      • Before calling the constructor of the parent class, you must ensure that the properties of this class have been initialized
      • Call the constructor of the parent class, assign space to the properties of the parent class, and set the initial value
      • If the constructor is overloaded and the Init method of the parent class is not overridden, the Init constructor is no longer available (which is the default) because the default constructor cannot allocate space to the properties of this class
//The person class has no ' initializer ', constructors can have multiple, default is Initclass person:nsobject{var name:string//overriding: The parent class has this method, the subclass is re-implemented and requires the Override keyword    Overrideinit () {//Initialize to Self.name: Allocate space, set initial valueName ="AAA"super.init ()}//Overload: Function name is the same, will be different parameters and number//overloads can set initial values from outside to their own propertiesInit (name:string) {//setting properties with the name of the parameterSelf.name =name//calling the constructor of the parent typesuper.init ()}}

    • KVC constructor function
      • When setting up the model, if it is an object, it is usually optional, created when needed, avoids writing the constructor, can optimize the code
      • If it is a basic data type, it cannot be set to optional, and the initial value is set, otherwise KVC will crash
      • If you need to set the property value using KVC, the property cannot be private
      • Before using the KVC method, you should call the Super.init method to ensure that the object is fully initialized
      • If the subclass does not override the method of the parent class, the method of the parent class is called directly when called
classperson:nsobject{//The Name property is optional, and many of the properties in OC are created when needed//such as Vc.view/tableviewcell.textlabel/detaillabel/imageview//Lazy Loading: Create again when neededvar name:string?//initialize a basic data type//var age:int? When using KVC, you will be prompted not to find the KEY for age//reason: int is a struct of a basic data type, only the basic data type in OCvar age:int =0       //if the private property is used, setting the value with KVC also fails to set the//if set to private property/method, external access is forbidden//private var title:string?Init (dict: [String:any]) {//ensure that the object is fully initializedSuper.init ()//You should call Super.init () before using the Self method setvaluesforkeys.//KVC method, is the OC method, at run time to the object message, requires that the object has been instantiated completeSetvaluesforkeys (dict)//Super.init ()    }     //overriding methods of the parent class    OverrideFunc SetValue (_ Value:any?, Forundefinedkey key:string) {        //set call super to fully overwrite the code of the parent class, preventing a crash when key-value pairs do not match    }}

    • Convenient constructor function
      • The convenience constructor allows nil to be returned, and the normal constructor must create the object
      • The purpose of the convenience constructor: To determine whether a given parameter meets the criteria, return nil if it does not meet the criteria, do not create an object, reduce the overhead of memory
      • Only the convenience constructor uses Self.init to construct the current object; Constructors that do not have a convenience keyword are responsible for creating the object, and vice versa, which is not responsible for the creation of the object itself.
      • If you want to use the properties of the current object in the convenience constructor, be sure to follow the Self.init
      • The convenience constructor cannot be overridden or called super
classstudent:nsobject{var name:string?var age:int=0    //Convenient constructor functionConvenience init?(name:string, age:int) {ifAge > -       {           returnNil}//You should call Self.init before using self to access name//self.name = name//instantiate the current objectSelf.init ()//execute to this self to allow access to the object's propertiesSelf.name =name}}

    • Destructors
      • Destruction of tracked objects
      • No func does not let the outside call
      • No () do not allow overloading
      • Called before the object is destroyed
      • Dealloc similar to OC
    deinit    {       /*         1. Tracking object Destruction          2. Must be released            -notification, no release will not crash, but will cause memory leaks            -KVO, do not release will crash            -   nstimer   /        cadisplaylink */    }

Swift Learning note nine: Class classes

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.