首页 > 代码库 > swift 导读
swift 导读
// Playground - noun: a place where people can playimport UIKit/***********************************************************//*** 一. A Swift Tour*///1.hello worldvar str = "Hello, playground"println("Hello, world \(str)")//2.返回值为函数的函数func addOne(number: Int) -> Int{ return 1 + number}func makeIncrementer() -> (Int -> Int){ return addOne}var increment = makeIncrementer()increment(7)//3.参数为函数的函数func hasAnyMatches(list: [Int], lessThanTen: Int -> Bool) -> Bool{ for item in list{ if lessThanTen(item){ return true } } return false}func lessThanTen(number: Int) -> Bool{ return number < 10}var numbers = [20, 19, 9, 12]hasAnyMatches(numbers, lessThanTen)//4.函数是一种特殊的闭包,除了大挂号,也可以使用in隔开闭包名和闭包的内容numbers.map({ (number: Int)-> Int in let result = 3 * number return result})//4.1 当闭包的类型已知时,闭包可以简写。numbers.map({ number in 3 * number})sort(&numbers) { $0 > $1 }/***********************************************************//*** 类对象*///1.定义一个类class Sharp: NSObject { var numberOfSides: Int = 0 var name: String init(name: String){//构造方法 self.name = name } func simpleDescription() -> String { return "A sharp with \(numberOfSides) sides" }}//2.继承, init, override, get, setclass EquilateralTriangle: Sharp {//等边三角形 var sideLength : Double //3.init方法 init(name: String, sideLength: Double) { self.sideLength = sideLength super.init(name: name) numberOfSides = 3 } //5.get方法和set方法 var perimeter: Double{//周长 get{ return 3 * sideLength } set{ sideLength = newValue / 3.0 } } func area() -> Double{ return sideLength * sideLength } //4.覆盖 override func simpleDescription() -> String { return "A EquilateralTriangle with sides of length \(sideLength)." }}//6. willSet, didSetclass Square: Sharp{ var sideLength: Double init(name: String, length: Double) { sideLength = length super.init(name: name) numberOfSides = 4 } override func simpleDescription() -> String { return "A Square with sides of length \(sideLength)." }}class traingleAndSquare: Sharp { var traingle: EquilateralTriangle{ willSet{ square.sideLength = newValue.sideLength } } var square: Square{ willSet{ traingle.sideLength = newValue.sideLength } } init(size: Double, name: String){ square = Square(name: name, length: size) traingle = EquilateralTriangle(name: name, sideLength: size) super.init(name: name) } }//
// Playground - noun: a place where people can playimport UIKitvar str = "Hello, playground"//定义一个协议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." }}var a = SimpleClass()a.adjust()let simpleDisc = a.simpleDescription//扩展extension Int: ExampleProtocol { var simpleDescription: String { return "The number \(self)" } mutating func adjust() { self += 42 }}//给Double类型扩展一个绝对值属性extension Double { var absoluteValue :Double { return self > 0 ? self : -1 * self }}var d: Double = -203.1println(d.absoluteValue)//范型(Generics)enum OptionalValue<T> {//可选值 case None case Some(T)}var possibleInt: OptionalValue<Int> = .None//OptionalValue.NonepossibleInt = .Some(100)//OptionalValue.Some(100)
swift 导读
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。