1234567 struct Rectangle { //属性 var width : Int var height : Int}var rectangle = Rectangle()rectangle.height = 4 1234567891011121314151617181920 struct Rectangle { //属性 var width : Int var height : Int var other : 12 //初始化方法 init(width: Int, height: Int) { self.width = width self.height = height } //函数 func fitsInside(_ other: Rectangle) -> Bool { return (width < other.width) && (height < other.height) } //自动计算值 var area: Int { return width * height }} Extensions 12345678910111213141516 struct Rectangle { var width : Int var height : Int init(width: Int, height: Int) { self.width = width self.height = height }}extension Rectangle { func fitsInside(_ other: Rectangle) -> Bool { return (width < other.width) && (height < other.height) } var area: Int { return width * height }} 泛型结构 123456789101112 struct NamedArray<Element> { var name: String var items: [Element]}//写法1let boardGame :NamedArray<String> = NamedArray(name: "Board Games", items:["Chess", "Go"])let primes: NamedArray<Int> = NamedArray(name: "Primes", items: [1,3,5,7,13])//写法2 (推荐)let boardGame = NamedArray(name: "Board Games", items:["Chess", "Go"])let primes = NamedArray(name: "Primes", items: [1,3,5,7,13]) class 1234567891011121314151617181920212223242526272829303132 class Fish { //属性 var name: String //初始化 init(name: String) { self.name = name } //方法 func swim() { print("I'm swimming.") }}//继承class FlyingFish: Fish { func fly() { print("fly") }}class ComplainingFish: Fish { var complaint: String //重写初始化方法 init(name: String, complaint: String) { self.complaint = complaint super.init(name: name) } 重写父类方法 override func swim() { print("123") super.swim() }} protocols 1234567891011121314151617181920212223242526 protocol Player { func takeTurn(on board: Board)}struct HumanPlayer: Player { func takeTurn(on board: Board) { }}struct ComputerPlayer: Player { var name: String var score: Int func takeTurn(on board: Board) { }}let player1 = ComputerPlayer.init(name: "fuck", score: 1)print(player1)//使用 extension 关键字会打印相关信息extension ComputerPlayer: CustomStringConvertible { var description: String { return "human player (name) has a score of (score)" }} Enumerations 1234 enum Alignment { case left case right} 等同于 123 enum Alignment { case left, right} 1 let textAlignment = Alignment.left 123456 switch textAlignment {case Alignment.left: print("1")case Alignment.right: print("2")} 等同于 123456 switch textAlignment {case .left: print("12")case .right: print("1")} 其他用法 12345678910111213 enum Alignment { case left(padding: Double), right(padding: Double), center}let textAlignment = Alignment.left(padding: 42.7)switch textAlignment {case .left(let padding): print("Left with (padding) pixels of padding")case .right(let padding): print("Left with (padding) pixels of padding")case .center: print("center")} 1234567891011 enum ServerAddress: String { case google = "https://www.google.com" case apple = "https://www.apple.com"}func findAddress(matchingQuery: String, from server: ServerAddress) { let serviceAddress = server.rawValue print(serviceAddress) }findAddress(matchingQuery: "11", from: .google) 123456789101112131415 enum SomeError: ErrorProtocol { case somethingWentWrong, somethingFailed}func doSomething() throws -> Data { progressBar.visible = true defer { progressBar.visible = false } let data: Data? do { data = try somethingThatMightFail() } catch SomeError.somethingWentWrong { data = nil } guard let result = summarize(data) else { throw SomeError.somethingFailed } return result} 赞微海报分享
近期评论