"Hey! White?" Lesson plans

Computed properties and packaging

Before we in class,Take a look at the previously mentioned "SoC" concept,We say this is called "Separation of Concerns",I translate it as separation of duties - the different sections that focus on their part。Or a target to achieve a goal。

The goal not only to make the code more modular and easier to maintain,Also let the system run more efficiently。So,We want the communication between objects becomes more standardized job。

Data Hiding

You go and see you writing framework code that, when used,Which gives you direct it shows the contents inside? They are exposed to a standardized interface for you,Like one of the black box,Into the glass、There are plastic battery,A camera came out。

So,Referring to such rules,We create a class during the time,Have to do。

Would you like to find themselves on the podium just do not wear pants?

In order to ensure our code is robust enough,As many properties are marked as private (private) is a good choice。

Together we look at an chestnuts:

I ask who the phone can call the "-1" this number?

You write wheels,You write a program,How much of this will not necessarily robust than phone class。There are always looking for caring people like this loophole,So,We should be avoided,For example, the use of non-negative "UInt" type handle complex problems,Or we can set up a calculated property to avoid property directly manipulate classes:

Swift language calculated property

In addition to property,Swift language also used to calculate the property getter and setter instead,It provides an operation can only be used to store other data itself can not attribute any variable,It is actually a method of packaging ...... summary,Thus it is actually more convenient to use。


All right,Then the concept of encapsulation,We go back and store the value in the chat,we all know,Variables can be placed in a variety of places to declare,For example, outside of class,Inside class,Even inside method。Corresponding to these three positions,These variables have different meanings。

Global Variables

Declare variables outside the class called Global Variables,With this thing will run your program and on the memory of them - until you close the program,It will only be freed,So,Use of global variables to be careful,Non-essential,Please do not use。














Original article written by Gerber drop-off:R0uter's Blog » Computed properties and packaging

Reproduced Please keep the source and description link:

By R0uter's Blog

The non-declaration,I have written articles are original,Reproduced, please indicate the link on this page and my name。

1 reply on “Computed properties and packaging”

Leave a Reply

Your email address will not be published. Required fields are marked *