Use of extensions in Swift

Source: Internet
Author: User
Import foundation/* Extension 1. Use extensions to add properties, methods, mutable methods, constructors, subscripts, nested type 2. You can make an existing type conform to one or more of the Protocols 3. Extensions are similar to the category of OC 4. Extensions are not derived subclasses and therefore do not support rewriting */ /*[modifier] Extension Existing type {//Add new function} modifier can be omitted, or private, internal, public one of them, type can be enumeration, struct and class one of them note: The new feature has been added to the existing type by extension, which All existing instances of the type are available in *//* by extending the existing type to conform to one or more protocol syntax formats: [modifier] Extension Existing type: Protocol 1, Protocol 2 {//Add new feature}*/extension string{    // Extend a computed property    var length:int {        get {            return Self.characters.count        }    }        //Extend a constructor    init (str: String)    {self        = str    }        //Extend a method    func getlength (), Int    {        return Self.characters.count    }        //Extend a static method, static    func show (), Void    {        print ("I ' m String")    }} var str:string = "Rinpe" var str2 = String (str: "Lala") print (str.length) print (Str.getlength ()) print (String.show ()) Print (STR2)

Use of extensions in Swift

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.