Methods and properties of classes and objects---lazy-loaded and private events---singleton and privatization constructors

Source: Internet
Author: User
Tags uikit

Object properties:

1,Private Modified properties: Only within this class access, classification and external access (completely private)

2.fileprivate Modified properties: Accessible in this class and classification, external inaccessible (partially private)

3. Properties modified directly with Let or Var : accessible (open) in this category, category, and outside

4. Only computed attributes can be declared in the classification, and storage properties cannot be declared

1     // properties: Can be accessed by the outside world 2     var name:string? 3     4     // Properties of private decoration: accessible only within this class, neither classified nor externally accessible 5     private var age:string? 6     7     // Fileprivate Modified properties: Accessible in this class and category, external inaccessible 8     fileprivate var gender:string?
1 Import UIKit2 3 classPerson:nsobject {4     5 }6 7 extension person{8     9     //only computed properties can be declared in a taxonomy, and stored properties cannot be declaredTen var desc:string{ One         return "I'm xiaoming ." A     } -}
Class Properties:

1. Use class-decorated properties in this class, called when the name.

2. Use the properties of the static modifier in the classification (note: Only computed properties can be defined in the taxonomy, and storage properties cannot be defined), called when the class name. Property name

1     //Class modifies this class of properties: Access through the class name. Property name2     classvar classattribute:string{3         return "I ' m the ClassAttribute."4     }5     6     //computed properties in the static modifier Category: Access by class name. property name7     Staticvar staticattribute:string{8         return "I ' m the Staticattribute."9}
Object method:

1, directly with the func modification, this class, classification, the outside world can be called (open)

2, with private decoration, only in this class can be called (completely private)

3, with fileprivate decoration, in this category, the classification can be accessed, external access (partial private)

1     //directly with the Func method, this class, the classification, the outside world can call2Func Sayhi ()Void {3         4     }5     //only in this class can you call the6     Privatefunc SayHi3 () {7         8     }9     //only in this class, in the category calledTenFileprivate func SayHi2 () (){ One      A}
Class method:

1. Use class to modify class methods in this class

2. Use the static modifier class method in the classification

1 classPerson:nsobject {2     3     //in this class, class methods are decorated with classes4     classFunc Sayhi () (){5Print"Hello world,i ' M the class method.")6     }7 }8 9 //extension equivalent to the classification in OCTen extension person{ One      A     //in classification, class methods are modified with static -     StaticFunc SayHello ()void{ -Print"Hello world,i ' m the static method.") the     } -}
Lazy loading with private events:

Lazy-loaded keywords with lazy decoration, there are three ways to create

The event that is modified with private , to be added to the button, the front must be decorated with @objc

1 Import UIKit2 3 classPerson:nsobject {4     5     //Lazy Load Mode one: Create directly6     PrivateLazy var Msglabel =UILabel ()7     8     //Lazy Load Mode Two: Create using object method, add self before method name9     PrivateLazy var content:string =self.createcontent ()Ten      One     //Lazy Load mode three: Using closures to create A     //Swift lazy loading with lazy decoration, the code executes only once, even if the object is nil, it will not be executed again -     //lazy Loading in OC is the override get method, which reloads when the object is nil -     //when lazy loading in Swift uses closures, closures can omit () in, and return objects directly the     PrivateLazy var Button:uibutton = { -Let button =UIButton () -          -         //use #selector to add events to the button in swift, note the notation with parameters (btn:) +         //use @selector to add an event to a button in OC -Button.addtarget (Self, Action: #selector (Buttonaction (btn:)), for:. Touchupinside) +          A         returnButton at     }() -      -      -     //Private modified event function, not found in Swift run loop -     //Workaround: Use OC, based on the runtime use KVC dynamic dispatch call this event, @objc tell the compiler to use OC mechanism to invoke this event -@objcPrivatefunc buttonaction (Btn:uibutton) { in          -     } to      +Func createcontent ()string{ -         return "what is created" the     } *}
Single example: Use static to modify the privatization of the constructor, the outside can not be created by the class name () object, only access to a single case, more rigorous
1 Import UIKit2 3 classPerson:nsobject {4     5     //Singleton object, modified with static6     StaticLet Currentperson:person =Person ()7     8     //using closures to create singleton objects9     StaticLet Sharedperson:person = {TenLet per =Person () One         returnper A     }() -      -     //Privatization constructor, the outside world can not create objects through the person (), can only access singleton objects thePrivateOverrideinit () { - Super.init () -     } -}

Methods and properties of classes and objects---lazy-loaded and private events---singleton and privatization constructors

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.