现在的位置: 首页 > 综合 > 正文

Swift tutorial 自学笔记(二):object,enum,struct

2018年04月19日 ⁄ 综合 ⁄ 共 3558字 ⁄ 字号 评论关闭

此篇是为了以后查资料方便,推荐学习的时候照着 The Swift Programming Language 中 tutorial章节敲一遍代码,对照描述理解透彻。新建工程推荐playground,可以直接看到结果

// Playground - noun: a place where people can play  
  
import UIKit  
  
  
var str = "Hello, playground"  
  
class Shape{  
var numberOfSides = 0  
var name:String  
init (name:String){  
self.name = name  
}  
deinit{  
self.name = "hh"  
}  
func simpleDescription() -> String {  
return "Shape(\(self.name)) with \(numberOfSides) sides"  
}  
}  
var shape = Shape(name: "hello")  
shape.numberOfSides = 7  
var shapeDescription = shape.simpleDescription()  
  
  
class Square: Shape {  
  
var userName:String{  
get{  
return self.name  
}  
set{  
self.name = newValue  
}  
}  
var sideLength:Double  
init(sideLengh: Double,name: String){  
self.sideLength = sideLengh  
super.init(name: name)  
numberOfSides = 4  
}  
func area() -> Double {  
return sideLength * sideLength  
}  
override func simpleDescription() -> String {  
return "A square(\(self.name)) with sides of length \(sideLength)."  
}  
}  
  
let test = Square(sideLengh: 56, name: "momola")  
test.area()  
test.simpleDescription()  
  
var square = Square(sideLengh: 45, name: "mmola")  
square.userName  
square.userName = "loomongdidi"  
square.userName  
  
class TriangleAndSquare {  
var triangle:Square{  
willSet{  
triangle.sideLength = newValue.sideLength  
}  
didSet{  
  
}  
}  
  
init(size:Double, name:String){  
triangle = Square(sideLengh: size, name: name)  
}  
}  
var triangleAndSquare = TriangleAndSquare(size: 52, name: "another test shape")  
triangleAndSquare.triangle.sideLength  
triangleAndSquare.triangle.simpleDescription()  
  
  
// ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^  
enum Rank:Int{  
case Ace = 1  
case Two,Three,Four,Five,Six,Seven  
case Jack,Queen,King  
func simpleDescription()-> NSString{  
switch self{  
case .Ace:  
return "ace"  
case .Jack:  
return "jack"  
case .Queen:  
return "queen"  
default:  
return String(self.rawValue)  
}  
}  
}  
let ace = Rank.Ace  
var returnValue = Rank.simpleDescription(ace)  
let aceRawValue = ace.rawValue  
let threeDescription = ace.simpleDescription()  
  
let converedRank = Rank(rawValue: 1)  
converedRank?.simpleDescription()  
  
enum Suit {  
case Spades, Hearts, Diamods, Clubs  
func simpleDescription()-> String {  
switch self {  
case .Spades:  
return "spades"  
case .Hearts:  
return "hearts"  
case .Diamods:  
return "diamods"  
case .Clubs:  
return "clubs"  
}  
}  
func color()-> String {  
switch self{  
case .Spades,.Clubs:  
return "black"  
case .Hearts,.Diamods:  
return "red"  
}  
}  
}  
let hearts = Suit.Diamods  
let heartsDescription = hearts.simpleDescription()  
let color = hearts.color()  
  
struct Card {  
var rank:Rank  
var suit:Suit  
func simpleDecription() -> String {  
return "The \(rank.simpleDescription()) of \(suit.simpleDescription())"  
}  
}  
let threeOfSpades = Card (rank: .Three, suit: .Spades)  
let threeofSpaderDescription = threeOfSpades.simpleDecription()  
  
  
enum ServerResponse {  
case Result(String,String)  
case Error(String)  
}  
let success = ServerResponse.Result("6:00 am", "8:09pm")  
let failure = ServerResponse.Error("Out of cheese.")  
switch failure {  
case let .Result(sunrise,sunset):  
let serverResponse = "Sunset:\(sunrise) and sunset is at \(sunset)"  
case let .Error(error):  
let serverResponse = "Failure...\(error)"  
}  
  
// mutating is aways needed in sturcture function, Class do not need because methods on a class can always modify the class  
protocol ExampleProtocol{  
var simpleDescription: String {get}  
mutating func adjust()  
}  
class SimpleClass: ExampleProtocol {  
var simpleDescription: String = "A very simple class."  
var anotherProperty: Int = 69105  
func adjust() {  
simpleDescription += " Now 100% adjusted."  
}  
}  
  
struct SimpleStructure: ExampleProtocol {  
var simpleDescription: String = "A very simple class."  
var anotherProperty: Int = 69105  
mutating func adjust() {  
simpleDescription += " Now 100% adjusted."  
}  
}  
  
// how to use mtating  
var a = SimpleClass()  
a.adjust()  
let aDescription = a.simpleDescription  
  
extension Int : ExampleProtocol{  
var simpleDescription: String{  
return "the number \(self)"  
}  
mutating func adjust() {  
self += 42  
}  
}  
8.simpleDescription  
  
// Generics  
func repeat<Item>(item:Item,times:Int)-> [Item]{  
var result = [Item]()  
for i in 0..<times{  
result.append(item)  
}  
return result  
}  
repeat("knock", 4) 

抱歉!评论已关闭.