In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to use swift methods". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Class instance method class Counter {var count = 0 func increment () {count + = 1} func increment (by count: Int) {self.count + = count} func reset () {count = 0}} let counter = Counter () print (counter.count) / / 0counter.increment () print (counter.count) / / 1counter.increment ( By: 10) print (counter.count) / / 11counter.reset () print (counter.count) / / 0 structure modify attribute mutating// structure modify attribute mutatingstruct Point {var x = 0.0 Y = 0.0 mutating func moveBy (x deltaX: Double, y deltaY: Double) {x + = deltaX y + = deltaY}} var point = Point (x: 1, y: 1) point.moveBy (x: 2, y: 2) print (point) / / Point (x: 3, y: 3) enumeration modify attributes mutating// enumeration modify attributes mutatingenum TriStateSwitch {case off, low High mutating func next () {switch self {case .off: self = .low case .low: self = .high case .high: self = .off}} var ovenLight = TriStateSwitch.lowprint (ovenLight) / / lowovenLight.next () print (ovenLight) / / highovenLight.next () print (ovenLight) / / off class method / / class method class Tool {static func getSize ()-> (Int Int) {return (100,100)} print (Tool.getSize ()) / / (100,100) structure body access subscript// structure body access subscriptstruct TimesTable {let multiplier: Int subscript (index: Int)-> Int {return multiplier * index}} let threeTimesTable = TimesTable (multiplier: 3) print (threeTimesTable [4]) / 12 access subscript// class access subscriptclass Matrix {let rows: Int with subscript Cols: Int var grid: [Double] init (rows: Int, cols: Int) {self.rows = rows self.cols = cols grid = Array (repeating: 0.0, count: rows * cols)} func indexIsValid (row: Int, col: Int)-> Bool {return row > = 0 & & row
< rows && col >= 0 & & col
< cols } subscript(row: Int, col: Int) ->Double {get {assert (row: row, col: col), "Index out of range") return grid [(row * cols) + col]} set {assert (indexIsValid (row: row, col: col), "Index out of range") grid [(row * cols) + col] = newValue}} var matrix = Matrix (rows: 2) Cols: 2) print (matrix.grid) / / [0.0,0.0,0.0,0.0] matrix [0,0] = 1.0matrix [0,1] = 2.0matrix [1,0] = 3.0matrix [1,1] = 4.0print (matrix.grid) / / [1.0,2.0,3.0,4.0] enumeration accesses subscript// enumeration with subscript access subscriptenum Direction: Int {case top, right, bottom Left static subscript (n: Int)-> Direction {return Direction (rawValue: n)!}} let right = Direction [1] print (right) / / right "how to use swift class methods" ends here Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.