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.