[IOS] extended extension and protocol and swiftextension in Swift

Source: Internet
Author: User

[IOS] extended extension and protocol and swiftextension in Swift

Together with the previous Playground summary code, the code has been transferred to github, and many comments have been written, so you can refer to it later. The main part of the Swift syntax is almost the same. Of course, there are supplements such as generics, Operator overloading, ARC, and closures.


1. extension)

Extended extension (similar to the category in OC, but Swift does not have a name), adds new features to the class even if you do not have the permission to get the original code.

Note: As long as an extension is defined, the extension is available for instances of this class.

Extension SomeType {// write the new function added to SomeType here}

1.1 extend attributes (only calculation attributes can be used)

// New computing attributes can be added for expansion, but storage attributes cannot be added (or attribute observation cannot be added ). extension Double {// Add the instance attribute var km: Double {return self * 0000000.0} var m: Double {return self} var cm for an existing type in the API: double {return self/100.0} let jjjlength = 1.m // vertex calculation between 1 and m, indicating the Double value of 1 let jlength_km = 1. kmprintln (10.km + 1.m)

1.2 extended Constructor

// You can customize your own constructor class MyClass {var a: Int init () {a = 10} extension MyClass {convenience init (var parm: Int) {// extended constructor self. init () println ("extended constructor ---> traversal constructor, \ (parm)") }}var myClass = MyClass (parm: 9)

1.3 Extension Method

The following describes how to extend the myIntFunc method in Int.

Extension Int {func myIntFunc () {println ("value: \ (self), Hahahaha! ")} 1. myIntFunc ()

1.3.1 how to modify an instance

You can modify the instance self by using the extension method. However, you must add mutating before the method.

Extension Double {mutating func myMoidfySelfValue {self = self * self // modify the value of the self instance} var d = 2.0d.myMoidfySelfValue ()

1.4 extend the nested type

Add a new nested type to an existing nested type. You can also extend the subscript (additional script) and so on.

Extension Character {enum Kind {// nested with an enumeration type case Big case Small} var k: Kind {if (String (self ). lowercaseString = "a") {return Kind. big} else {return Kind. small }}var ch: Character = "a" ch. k // return an enumerated value Kind. big

Ii. protocol)

Methods and attributes that can be defined are implemented by specific classes.

The protocol in Swift can be implemented by class, enumeration, and struct.

Protocol SomeProtocol {// protocol content} class SomeClass: SomeProtocol {// implementation protocol, multiple protocols can be implemented}

2.1 Requirements for properties/methods/mutation methods in the Protocol

2.1.1 attribute requirements

Protocol AnotherProtocol1 {// class indicates class members (static in struct/enumeration) class var property: Int {get set} // get, set indicates readable and writable} class AnotherClass1: anotherProtocol1 {class var property: Int {// implement the attribute get {return 10} set {}} IN THE PROTOCOL {}}}

2.1.2 method requirements

// The default parameter is not supported. there is no way to implement it. protocol AnotherProtocol2 {func myFunc ()-> Int // declare not implemented only} class AnotherClass2: AnotherProtocol2 {func myFunc ()-> Int {// implementation method return 10 }}

2.1.3 mutation method requirements

A method that can change the instance type within a method or function is called a mutating method)

In the class, mutating can be avoided, but it must be written in China in struct and enumeration.

Protocol Togg {mutating func togg ()} enum OnOffSwitch: Togg {case Off, On mutating func togg () {// change the Instance value switch self {case. off: self = On case. on: self = Off }}var lightSwitch = OnOffSwitch. offlightSwitch. togg () // The value is On.

2.2 protocol type.

The protocol can also be used as a type. This is the same as the function.

1. can be used as a parameter/Return Value Type

2. It can be used as a constant, variable, or attribute type.

3. Can be used as arrays/dictionaries and other element types

Protocol MyRect {func myLuckNumber ()-> Int} class MyRectImp: MyRect {func myLuckNumber ()-> Int {return 10} class Dice {let sides: Int var gener: myRect // as the type init (sides: Int, gener: MyRect) {// as the parameter self. sides = sides self. gener = gener} var dice = Dice (sides: 6, gener: MyRectImp () dice. gener. myLuckNumber () // returns 10

For the sample code, see: http://github.com/xn4545945/SwiftLearning


Refer:

The Swift Programming Language

Apple Dev Center


Reprinted please indicate the source: http://blog.csdn.net/xn4545945






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.