Swift Learning (4 lazy loading, computed properties, reflection mechanism)

Source: Internet
Author: User

Lazy loading, computed properties, reflection mechanism

1. Lazy Loading:

Purpose: 1. Delay creation, load when needed, save memory space

2. Avoid the development of the problem of understanding the package (important!!!) )

Knowledge: 1. All UIView and subclasses are in development, once the function is rewritten, the Initwithcoder function must be implemented to provide two channels, and Xcode will now be prompted.

2. Simple notation for lazy loading in swift

Lazy var Label:uilabel = UILabel ()

3. Lazy loading is essentially a closure, the complete wording is as follows:

{} wrapper code () execution code

Lazy var Labe = {()->uilabel in

Let L = UILabel ()

return L

}()

Note that understanding is good because the tips in closures are not very good in everyday development, and you need to be aware of circular references

4. The difference between lazy loading and OC:

(1) in OC, the Get method is used to implement lazy loading.

(2) in OC, as long as the set to nil, can be re-loaded by the Get method, and in swift lazy loading can only be loaded once, if set to nil, you can not directly through the lazy load new creation

2. Computed Properties

Guide:

1.getter & Setter for demonstration only, daily development without:

Private var _name:string?

var name:string{

get{

return _name

}

set{

_name = newvalue

}

}

2. Implementation of ReadOnly: Override Getter method, implemented by write-only get method.

var title:string{

get{

Return "Mr Lao Wang"

}

}

Computed properties:

1.readonly Shorthand: Direct return

var title:string{

Return "Lao Wang"

}

In fact, this is a computed attribute: the content itself is not saved, it is obtained by calculation. Similar to a function, no parameter has only a return value, and each calculation is recalculated once.

3. The difference between lazy loading and computed properties

1. The code is very similar!!! Lazy loading is just one more ' lazy ' and ' = '

2. Lazy loading will be executed at the time of the first visit, the result will be saved to the property after the closure execution, and the closure will not be re-executed when the second call is done. Computed properties Each access will execute a closure

4. Using the Override set method to change the UI by assigning values to the model

var Person:person? {

Didset {

The Name property has a value and can be used directly

Self.text = person?. Name

}

}

5. Namespaces & Reflection Mechanisms

1. The concept of reflection mechanism

(1) In any class, all the properties and methods of the class can be obtained by means of a method.

(2) For any object, you can call any of his methods and properties

(3) The method function of dynamic access information and dynamic calling object becomes the reflection mechanism of Java language.

2. How to use the reflection mechanism in OC

(1) Use the Nsclassfromstring method to get the class using a string

(2) Ismemberofclass

(3) Iskindofclass

(4) Conformstoprotorol

(5) Respondstoselector

(6) Performselector or Obj_msgsend

3. Matters of the doctrine:

(1) There are namespaces in Swift

(2) global sharing in the same namespace

(3) The third framework uses Swift if you drag directly into the project, you may have a revisit from the same namespace

(4) In Swift, nsclassfromstring is required to add a namespace before, for example: let Clsname = "reflection mechanism. Viewcontroller "

(5) The important purpose of reflection is to understand the coupling

(6) Can search "reflection mechanism and factory method" to continue learning reflection mechanism

4. Dynamically Acquire namespaces:

4.1 In Info.plist bundle name is namespace

4.2 Info.plist needs to be read from Bundle.main.infoDictionary

4.3 So the namespace is: bundle.main.infodictionary["Cfbundlename"]

4.4 Code simplification through write classification:

4.4.1 through function calls

4.4.2 through computed properties. This method can increase the readable type of code.

Demo:https://github.com/fushengit/learn-swift

Swift Learning (4 lazy loading, computed properties, reflection mechanism)

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.