Swift notes (18)--Extensions

Source: Internet
Author: User

ExtendedExtension allows us to add to an existing class, struct, enum, and so on? New features, including properties and methods, even constructors, subscripts, support protocols, etc...
Even if we do not get the source of the class, struct, enumeration, we can still extend it ...
When I saw this, I was a little excited ... But after the calm down, I thought it was a big one.Fly line, fly line , fly Line。。。 This makes our code difficult to understand comprehensively (always worry about whether there is any extension, I did not read it.) Who would write a flying line to reach his unspeakable secret, so we have to write in the code specification: the extension must be written next to the implementation of the class. But what about classes that don't have code? We also have to specify that the file name must be a similar xxxxextensions.swift or xxxxdelegate.swift name ... Why not let the user through the proxy mode to do this kind of thing, but is to provide such a anti-code anti-human characteristics ... )
In any case, since the provision of such things, it is still a record of this wonderful way.
Grammar:extensionMyClass {//To extend the properties, methods, etc functions written here}
extensionMyClass: MyProtocol{//protocol in the notes in the back, it's just a mention .To extend the properties, methods, and so on functions written here}
what the extension can do:1. Join? computed and computed static properties2. Defining instance methods and type methods3. Provide a new constructor4. Define subscript5. Define and use a new nested type6. Keeping an existing type in compliance with an agreement
Before writing an example, I found that int did not have the ToString method, so we are now able to have an int with the ToString method by using the powerful features of the extension:
extension Int {
functoString ()String {
varTmparray = ["0","1","2","3","4"
,"5","6","7","8","9"]
varTmpnum = Self
vartmpstring =""
whiletmpnum >0 {
tmpstring = tmparray[tmpnum%Ten] + tmpstring
Tmpnum/=Ten
}
returntmpstring
}
}

varMyNum:Int = 6234232
println (MyNum.toString())

Note: This is just a toy (demo sample), so consider a better way to use it if you are using the project. As for the constructors, subscripts, mutating methods, nested types mentioned in the documentation, nothing more. Simply, just write your own play. Not listed.

Swift notes (18)--Extensions

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.