Swift Learning (3 object-oriented)

Source: Internet
Author: User

Swift Object-oriented

1. In swift, by default in the same project (under the same namespace), all classes are shared, and no import is required for all of the properties Var can also be accessed directly to the

2. In swift, all classes have a namespace by default, which is the project name

3. () Alloc init. One of the Init () constructors in Swift is to allocate memory space to member variables and initialize

4. Constructor notation:

A: How to construct a required attribute

1. Assign a memory space to your own properties and set the initial value

2. Call the parent class's constructor to assign the Lei Cun space to the parent class's property to set the initial value

3.NSObject no attributes, only one member variable ISA

5. Overloaded Function Method: The function name is the same, but the parameters and the number are different. You can assign an initial value to your own property when it is called externally. OC is not overloaded

6. If the constructor is overloaded and the Init method of the parent class is not implemented, the Init () constructor is no longer available. Because the default constructor is not able to allocate memory space to the properties of this class

7. The console uses the P command to obtain object information

8. Constructor (KVC), memory is very valuable in mobile phone development, some properties do not have to allocate space.

Lazy loading: Create only when needed

If it is a model attribute, if it is an object, it is usually an optional code that simplifies writing the constructor.

The KVC method is the OC method, which sends a message to the object at run time, requiring that the object has been instantiated for completion!

Before using the KVC method, you should use Spuer.init () instantiation

Initialization of the basic data type is not optional and must be set to the initial value in advance. Or KVC will crash.

If it is private, it will not be set when using KVC setting value, KVC will crash

In Swift, properties and methods are set to private, which is absolutely forbidden from external access! Does not get all the methods and properties through the runtime, as in OC

As with OC, the key in the dictionary should be rewritten if the attribute with no formation in the class corresponds to the Setvalueforundifindkey

9.Swift Runtime Load Properties list: OC-like

1. Get a list of properties for ' class '

2. Iterating through an array

3. Get the attribute according to the subscript: objc_property_t?

4. Get the C-language string for the name of the property

5. Convert to String

6. Releasing the C language object

7. It is recommended to use the guard in turn to determine options

10. Basic data type, no optional in OC, if defined as optional, runtime is also not available, using KVC will crash

11.private properties, using Run-time yes, same get not properties, using KVC will crash

12. All parameters of the closure are to be written by themselves, OC is directly brought into

13. Practice using Urlsession to load Baidu

14. Convenient constructor function

1. Determine the condition and instantiate the object only if the condition is met

2. Simplifying the creation of objects

3. Not responsible for the creation and initialization of attributes

4. The convenience constructor allows nil to be returned, the normal constructor must create the object, determine whether the given parameters match the criteria, return nil if it does not, and not create the object, reducing memory overhead!

5. Construct the current object using ' Self.init ' only in the convenience constructor

6. Constructors that do not have the CONVENIENCE keyword are responsible for creating the object, and vice versa, which is not responsible for the creation of the object itself.

7. If you want to use the properties of the current object in the convenience constructor, be sure to follow the Self.init

15. Refactoring the addition calculator with the convenience constructor

How to do 1.Swift classification

2. Use convenient constructors to write default values for properties of some controls in a categorical way

16. Combat 1: Write a Personal Address Book

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

Swift Learning (3 object-oriented)

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.