//: Playground-noun:a Place where people can playImport UIKit//----Extension extension---------////function: Extend new functions (methods and properties) to existing classes, structs, enumerations//extensions in Swift do not have a name//1. Extended properties: Only calculated properties can be added and storage properties cannot be addedextension UIView {//var Issuperview = Falsevar height:cgfloat {Get { returnSelf.frame.size.height}Set{self.frame.size.height=NewValue}} }var MyView= Uiview.init (Frame:cgrectmake ( -, -, -, -)) Myview.height= $print (myview.height)//2. Extension methodsextension String {//Interception of String substringsFunc Substringfromstartindexandendindex (Start:int, End:int)String {var count=0var result="" for Char inchSelf.characters {ifCount >=Start {result+="\ (char)" } ifCount >=End { Break} Count++ } returnresult}//bool converted to string StaticFunc Stringwithbool (Value:bool)String {return "\ (value)"}}var Str="Hello"Str.substringfromstartindexandendindex (0, End:4) String.stringwithbool (true)
Swift extended Extension_011-swift Extension Basic use