Methods and attributes of classes and objects --- lazy loading and private events --- singleton and private constructor

Source: Internet
Author: User

Methods and attributes of classes and objects --- lazy loading and private events --- singleton and private constructor
Object Property:

1,PrivateModified attributes: they can only be accessed within the class, but cannot be accessed by both the category and the outside (completely private)

2,FileprivateModified attributes: can be accessed in this class and category, and cannot be accessed outside (partially private)

3,Directly modify with let or varAttribute: can be accessed (open) from the class, category, and outside)

4. Only calculation attributes can be declared in classification, and storage attributes cannot be declared.

1 // attribute: can be accessed by the outside world 2 var name: String? 3 4 // attributes modified by private: they can only be accessed within the class, but cannot be accessed both within the category and outside. 5 private var age: String? 6 7 // attributes modified by fileprivate: this class and category are accessible, and external access is not allowed. 8 fileprivate var gender: String?
1 import UIKit 2 3 class Person: NSObject {4 5} 6 7 extension Person {8 9 // the class can only declare the calculation attribute, but cannot declare the storage attribute 10 var desc: string {11 return "I am James" 12} 13}
Class attributes:

1. UseClassClass Name. attribute name

2. Use in classificationStaticModified attributes (Note: Only calculation attributes can be defined in a category, but storage attributes cannot be defined). Class Name. attribute name is called.

1 // class: class Name. access 2 class var classAttribute: String {3 return "I'm the classAttribute. "4} 5 6 // modify the calculation attribute in the static classification: Use the class name. access 7 static var staticAttribute: String {8 return "I'm the staticAttribute. "9}
Object method:

1. directly useFuncModify, this class, category, and external can be called (open)

2. UsePrivateModification, which can be called only in this class (completely private)

3. UseFileprivateModification, which can be accessed in the class and category, but cannot be accessed outside (partially private)

1 // directly use the func method. This class, category, and outside world can call 2 func sayHi () -> Void {3 4} 5 // only in this class can call 6 private func sayHi3 () {7 8} 9 // only 10 fileprivate func sayHi2 ()-> () {11 12} is called in this class and category}
Class method:

1. UseClassModifier

2. Use in classificationStaticModifier

1 class Person: NSObject {2 3 // in this class, class methods use class to modify 4 class func sayHi ()-> () {5 print ("Hello world, i'm the class method. ") 6} 7} 8 9 // extension is equivalent to 10 extension Person {11 12 in OC // in classification, class method uses static to modify 13 static func sayHello () -> Void {14 print ("Hello world, I'm the static method. ") 15} 16}
Lazy loading and private events:

Lazy loading keywordsLazyThree creation methods are available.

UsePrivateTo add a modified event to a button, you must use@ ObjcModify

1 import UIKit 2 3 class Person: NSObject {4 5 // Method 1: Create 6 private lazy var msgLabel = UILabel () 7 8 // Method 2: create with object method. Add self 9 private lazy var content: String = self before method name. createContent () 10 11 // lazy loading method 3: Use closures to create 12 // use lazy for swift lazy loading. The code is executed only once, even if the object is nil, it will not re-execute the "13 // OC" lazy loading. It is to rewrite the get method. When the object is nil, it will re-load the 14 // swift lazy loading and use the closure method, the closure can omit () in and return the object directly. 15 private lazy var button: UIButton = {16 let button = UIButton () 17 18 // use # selector in swift to add events to the button. Note that @ selector is used in btn :) 19 // OC to add events to the button 20 button. addTarget (self, action: # selector (buttonAction (btn :)), :. touchUpInside) 21 22 return button23} () 24 25 26 // private modified event function, which cannot be found in the Swift running loop 27 // solution: use oc, use kvc to dynamically dispatch and call the event based on the runtime. @ objc tells the compiler to use the oc mechanism to call this event 28 @ objc private func buttonAction (btn: UIButton) {29 30} 31 32 func createContent ()-> String {33 return "Created Content" 34} 35}
Singleton: private constructor modified with static. You can only access Singleton without creating an object through class name ().
1 import UIKit 2 3 class Person: NSObject {4 5 // singleton object, use static to modify 6 static let currentPerson: Person = Person () 7 8 // use the closure method to create a singleton object 9 static let sharedPerson: Person = {10 let per = Person () 11 return per12} () 13 14 // private constructor, you cannot create an object using Person (). You can only access the 15 private override init () {16 super. init () 17} 18}


Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.