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