Extensions and protocols for Swift languages (interfaces)

Source: Internet
Author: User

/*

Type conversions

1. Conversion between two types without any relationship

2. Upward and downward transformation in the chain of succession

*/

The first form of

Let i = 3

Let str = "\ (i)"

Let str2 = String (i)

The second form of

Class Parent {

var p = 1

}

Class Child:parent {

var c = 2

}

As is used to convert

Let c:parent = Child ()

Let CC = c as? Child

Cc?. C

If let CCC = C as? Child {

Ccc.c

}

Acc. C

is to determine whether this instance belongs to this class

Let result = C was child

Let result2 = C is Parent

Anyobject: Represents the type of any class

Any: arbitrary types, such as structs, enumerations, classes, functions, etc.

Class A {

var aa:int = 1

}

Class B {

var bb:int = 2

}

There is no top-level parent in Swift saying (Java-like object)

Let data: [Anyobject] = [A (), a (), B (), B ()];

var any1count = 0

var any2conut = 0

For item in data {

If Item is A {

any1count++

}else If Item is B {

any2conut++

}

If let ins = Item as? A

Ins.aa

}

}

Any1count

Any2conut

Let data1: [Anyobject] = [A (), a ()];

Let d = data1 as! A

D[1].aa

/*

Extended:

1. Can be used to extend the class, structure, enumeration, protocol (even if these you do not have source code, the system can be)

2. Can increase but cannot rewrite existing functions

***********************************

1. You can add instance calculation properties and static computed properties (such as adding some calculated properties to a double type that represent the length units)

2. Adding instance methods and class methods

3. Provide a new initialization method.

1. Other types can be used as parameters for new Init

2. or other init options

3. Add init to a value type by extension, or you can keep various init that are automatically added by the compiler in the original value type

4. Add subscript

5. Enable an existing type to implement a protocol

No:

1. Cannot add storage properties (should be the effect on init)

2. Cannot add observer to attribute

3. You cannot add a specific initializer to a class

4. You cannot add destructors to a class

*/

Extend method to system int m km

Extension Int {

var M:int {

Return 1

}

var Km:int {

Return self * 1000

}

}

Let II = 20

Ii.m

ii.km

/*

Protocol: Is the meaning of the interface

When adding properties:

1. Specify only the name and type of the property, and do not specify whether the property is stored or computed

2. Static properties can only be modified with static in the protocol. But the implementation type can also be used with the static class

3. Read-only property var a:int {get}

4. Read-write property var b:int {get Set}

5. The properties in the agreement are always declared with Var, but can be implemented as a let

When adding a method:

1. No curly braces required

2. Support variable length parameters

3. The default value cannot be specified in the agreement

4. Static methods are also modified with static (in the Protocol)

5. The method in the protocol can be added mutating, in the class to be implemented, you can not need mutating, in the value type

We must add mutating in the realization of the middle.

*/

Extensions and protocols for Swift languages (interfaces)

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.