Swift extended Extension_011-swift Extension Basic use

Source: Internet
Author: User

//: 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

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.