Introduction and examples of Swift-subscript scripting methods

Source: Internet
Author: User
Tags assert

Implement a custom string class that can be easily indexed to get a character value, or a portion of a string. It is also possible to assign values to a part by index.

Class substring{var str:string = "" Init (str:string) {self.str = str; }/** Subscript Script: Get/Set part string **/subscript (Start:int, length:int), string {get{Let Index 1 = Str.index (Str.startindex, offsetby:start) Let Index2 = Str.index (index1, offsetby:length) let        Range = Range (uncheckedbounds: (Lower:index1, Upper:index2)) return str.substring (With:range)}  set{Let tmp = str str = "" var s = "" var e = "" For (idx, item)                In tmp.characters.enumerated () {if (idx < start) {s + = "\ (item)"                } if (idx >= start + length) {e + = "\ (item)" }} str = s + newvalue + e}}/** Subscript script: Get/Set character **/subscript (index:int) string {get{return string (StR[str.index (Str.startindex, Offsetby:index)])} set{let tmp = str str = "" For (idx, item) in tmp.characters.enumerated () {if idx = = Index {str + = "\ (newvalue) "}else{str + =" \ (item) "}}}}} let str = Substri Ng (str: "baibai.com") print (str[7,3])//Get string: Comprint (Str[7])//Get character: c str[7,3] = "com"//Set part string str[0] = "H"//Set part character PR Int (str[0,10])/baibai.com

  You can also add an index function directly to the string class by extending the class

Extension string{subscript (start:int, length:int), String {get{let index1 = Self.index (Self.startindex, Offsetby:start) Let Index2 = Self.index (index1, offsetby:length) Let range = Ran            GE (uncheckedbounds: (Lower:index1, Upper:index2)) return self.substring (With:range)} set{ let tmp = self var s = "" var e = "" For (idx, item) in tmp.characters.enumerated                () {if (idx < start) {s + = "\ (item)} if (idx >= start + length) {e + = "\ (item)"}} s Elf = s + newvalue + e}} subscript (Index:int), string {get{return string (SE            Lf[self.index (Self.startindex, Offsetby:index)])} set{let tmp = self self = "" For (idx, item) In tmp.characters.enumerated () {if idx = = Index {self + = "\ (newvalue)"} else{self + = "\ (item)}}}}} var str =" baibai.com "Print (Str[7, 3]) print (str[7]) str[7,3] = "COM" str[0] = "H" Print (str[0,10])

A two-dimensional array is simulated to some extent using a one-dimensional array combined with subscript method

Class Matrix {Let    rows:int, Columns:int    var grid: [Double]         init (rows:int, columns:int) {        self.rows = Rows        self.columns = columns        Grid = Array (repeating:0.0, count:rows * columns)    }         func Indexisvalidforrow (Row:int, column:int), Bool {        return row >= 0 && row < rows && Column & gt;= 0 && Column < columns    }         subscript (Row:int, column:int), Double {        get {            assert (in Dexisvalidforrow (Row:row, Column:column), "Index Out of Range")            return grid[(Row * columns) + column]        }        S  ET {            assert (Indexisvalidforrow (Row:row, Column:column), "Index Out of Range")            grid[(Row * columns) + column] = NewValue        }}    } Let value = Matrix (rows:20,columns:20) value[10,10] = 20print (value[10,10])//20

Swift-Subscript Script methods Introduction and examples

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.