Swift notes (18)-extensions

Source: Internet
Author: User

Extended extensions allow us to add new features to an existing class, struct, enumeration, and other types, including attributes and methods, even constructors, subscripts, and support protocols...
Even if we don't get the class, struct, and enumeration of the source code, we can still extend it...
When I saw this, I was a little excited... But after the calm, I thought this TMD was a big flying line, flying line, and flying line... This makes our code hard to understand comprehensively (always worry about where there are extensions, I didn't read it .. Who will write a flying line out to bring out their secrets, so we have to write in the "code specification": the extension must be closely related to the implementation of the class. But what about classes without code? We also need to specify a name for the file name, such as xxxxExtensions. swift or xxxxDelegate. swift... Why not let the user do this through the proxy mode? Instead, it provides such a anti-code anti-human feature ...)
In any case, since such a thing is provided, record this wonderful way.
Syntax: extension MyClass {// attributes, methods, and other functions to be extended are written here}
Extension MyClass: MyProtocol {// the protocol will be added in the subsequent notes. Here we will only mention the attributes, methods, and other functions to be extended}
What extensions can do: 1. add computing attributes and computing static attributes 2. define the instance method and type method 3. provide a new constructor 4. define subscript 5. define and use a new nested type 6. make an existing type comply with a protocol
When I wrote an example, I found that Int does not have the toString method. Therefore, we can now use the powerful extended functions to enable the toString Method for Int:
Extension Int {
Func toString ()-> String {
Var tmpArray = ["0", "1", "2", "3", "4"
, "5", "6", "7", "8", "9"]
Var tmpNum = self
Var tmpString = ""
While tmpNum> 0 {
TmpString = tmpArray [tmpNum % 10] + tmpString
TmpNum/= 10
}
Return tmpString
}
}

Var myNum: Int = 6234232
Println (myNum. toString ())

Note: This is just a toy (example). If you are using a project, consider better writing. The constructor, subscript, mutating method, and nested type mentioned in this document are nothing more than this .. It's easy. Just write it and play it. No.

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.