Swift5 https://docs.swift.org/swift-book/GuidedTour/GuidedTour.html
import UIKit
var str = "Hello, playground"
//介绍swift5相关的语法
//********************常量和变量 /* 1.常量定义时必须赋初值, 2.变量定义时必须加 “!” or “?” 3.可以在一行中声明多个常量或者多个变量,用逗号隔开: */ let maximumNumberOfLoginAttempts = 10 var currentLoginAttempt = 0 var x = 0.0, y = 0.0, z = 0.0
var sgfdfd:Int? var welcomeMessage: String! var welcomeMessageInt: Int?
var optionalString: String? = "Hello" print(optionalString == nil) // Prints "false"
var optionalName: String? = "John Appleseed" var greeting = "Hello!" if let name = optionalName { greeting = "Hello, \(name)" }
//********************条件语句 //Switch let vegetable = "red pepper" switch vegetable { case "celery": print("Add some raisins and make ants on a log.") case "cucumber", "watercress": print("That would make a good tea sandwich.") case let x where x.hasSuffix("pepper"): print("Is it a spicy \(x)?") default: print("Everything tastes good in soup.") } // Prints "Is it a spicy red pepper?"
//for-in //字典是无序的!!! let interestingNumbers = [ "Prime": [2, 3, 5, 7, 11, 13], "Fibonacci": [1, 1, 2, 3, 5, 8], "Square": [1, 4, 9, 16, 25], ] var largest = 0 for (kind, numbers) in interestingNumbers { print(kind) for number in numbers { if number > largest { largest = number } } } print(largest)
//while-repeat var n = 2 while n < 100 { n *= 2 } print(n) // Prints "128"
var m = 2 repeat { m *= 2 } while m < 100 print(m) // Prints "128"
//You can keep an index in a loop by using ..< to make a range of indexes. var total = 0 for i in 0..<4 { total += i } print(total)
//********************数组和字典 //创建方式一 var shoppingList = ["catfish", "water", "tulips"] shoppingList[1] = "bottle of water"
var occupations = [ "Malcolm": "Captain", "Kaylee": "Mechanic", ] occupations["Jayne"] = "Public Relations" shoppingList.append("blue paint") print(shoppingList) //创建方式二 let emptyArray = [String]() let emptyDictionary = [String: Float]() let individualScores = [75, 43, 103, 87, 12] //注意if 语句的使用 var teamScore = 0 for score in individualScores { if score > 50 { teamScore += 3 } else { teamScore += 1 } } print(teamScore) // Prints "11"
//********************Functions and Closures /* 函数实际上是闭包的一种特殊情况: 方法名可以一样,但是方法里的参数设置不能一样,否则报错 */
///简单方法 func greet(person: String, day: String) -> String { return "Hello \(person), today is \(day)." } func greet(_ person: String, on day: String) -> String { return "Hello \(person), today is \(day)." } func greet(_ person: String, ooon day: String) -> Int { return 15 } greet(person: "Bob", day: "Tuesday") greet("John", on: "Wednesday") greet("Bill", ooon: "TOne")
///返回多个参数的方法[通过tuple 实现] func calculateStatistics(scores: [Int]) -> (min: Int, max: Int, sum: Int) { var min = scores[0] var max = scores[0] var sum = 0 for score in scores { if score > max { max = score } else if score < min { min = score } sum += score } return (min, max, sum) } let statistics = calculateStatistics(scores: [5, 3, 100, 3, 9]) print(statistics.sum) // Prints "120" print(statistics.2) // Prints "120"
///函数可以嵌套。嵌套函数可以访问在外部函数中声明的变量。可以使用嵌套函数在长函数或复杂函数中组织代码。 func returnFifteen() -> Int { var y = 10 func add() { y += 5 } add() return y } returnFifteen() ///函数可以返回另一个函数作为它的值。 func makeIncrementer() -> ((Int) -> Int) { func addOne(number: Int) -> Int { return 1 + number } return addOne } var increment = makeIncrementer() increment(7)
///一个函数可以接受另一个函数作为它的参数之一。 func hasAnyMatches(list: [Int], condition: (Int) -> Bool) -> Bool { for item in list { if condition(item) { return true } } return false } func lessThanTen(number: Int) -> Bool { return number < 10 } var numbers = [20, 19, 7, 12] hasAnyMatches(list: numbers, condition: lessThanTen)
///函数实际上是闭包的一种特殊情况: numbers.map({ (number: Int) -> Int in let result = 3 * number return result }) ///您有几个选项可以更简洁地编写闭包。当闭包的类型已知时,例如委托的回调,您可以省略它的参数类型、返回类型,或者两者都省略。单个语句闭包隐式地返回它们唯一语句的值。 let mappedNumbers = numbers.map({ number in 3 * number }) print(mappedNumbers) // Prints "[60, 57, 21, 36]"
//********************Objects and Classes //一个简单的类 class Shape { var numberOfSides = 0 func simpleDescription() -> String { return "A shape with \(numberOfSides) sides." } }
var shape = Shape() shape.numberOfSides = 7 var shapeDescription = shape.simpleDescription()
//一个有init 的类 class NamedShape { var numberOfSides: Int = 0 var name: String init(name: String) { self.name = name } func simpleDescription() -> String { return "A shape with \(numberOfSides) sides." } } //实现继承的类 class Square: NamedShape { var sideLength: Double init(sideLength: Double, name: String) { self.sideLength = sideLength super.init(name: name) numberOfSides = 4 } func area() -> Double { return sideLength * sideLength } override func simpleDescription() -> String { return "A square with sides of length \(sideLength)." } } let test = Square(sideLength: 5.2, name: "my test square") test.area() test.simpleDescription()
///除了存储的简单属性外,属性还可以有getter和setter。 class EquilateralTriangle: NamedShape { var sideLength: Double = 0.0 init(sideLength: Double, name: String) { self.sideLength = sideLength super.init(name: name) numberOfSides = 3 } var perimeter: Double { get { return 3.0 * sideLength } set { sideLength = newValue / 3.0 } } override func simpleDescription() -> String { return "An equilateral triangle with sides of length \(sideLength)." } } var triangle = EquilateralTriangle(sideLength: 3.1, name: "a triangle") print(triangle.perimeter) // Prints "9.3" triangle.perimeter = 9.9 print(triangle.sideLength) // Prints "3.3000000000000003"
///如果您不需要计算属性,但仍然需要提供在设置新值之前和之后运行的代码,那么使用willSet和didSet。只要值在初始化器之外发生更改,就会运行您提供的代码。例如,下面的类确保三角形的边长总是与其正方形的边长相同。 class TriangleAndSquare { var triangle: EquilateralTriangle { willSet { square.sideLength = newValue.sideLength } } var square: Square { willSet { triangle.sideLength = newValue.sideLength } } init(size: Double, name: String) { square = Square(sideLength: size, name: name) triangle = EquilateralTriangle(sideLength: size, name: name) } } var triangleAndSquare = TriangleAndSquare(size: 10, name: "another test shape") print(triangleAndSquare.square.sideLength) // Prints "10.0" print(triangleAndSquare.triangle.sideLength) // Prints "10.0" triangleAndSquare.square = Square(sideLength: 50, name: "larger square") print(triangleAndSquare.triangle.sideLength) // Prints "50.0"
///当处理可选值时,您可以编写?在方法、属性和下标等操作之前。如果之前的值是?是nil吗?被忽略,整个表达式的值为nil。否则,可选值将被打开,并且在?作用于打开的值。在这两种情况下,整个表达式的值都是一个可选值。 let optionalSquare: Square? = Square(sideLength: 2.5, name: "optional square") let sideLength = optionalSquare?.sideLength
//********************Enumerations and Structures //使用枚举创建枚举。与类和所有其他命名类型一样,枚举可以具有与其关联的方法。 enum Rank: Int { case ace = 1 case two, three, four, five, six, seven, eight, nine, ten case jack, queen, king func simpleDescription() -> String { switch self { case .ace: return "ace" case .jack: return "jack" case .queen: return "queen" case .king: return "king" default: return String(self.rawValue) } } } let ace = Rank.ace let aceRawValue = ace.rawValue //使用init?(rawValue:)初始化器从原始值生成枚举的实例。如果没有匹配的秩,则返回匹配原始值的枚举用例或nil。 if let convertedRank = Rank(rawValue: 3) { let threeDescription = convertedRank.simpleDescription() }
//枚举的大小写值是实际值,而不仅仅是编写原始值的另一种方式。事实上,在没有有意义的原始值的情况下,您不需要提供一个。 enum Suit { case spades, hearts, diamonds, clubs func simpleDescription() -> String { switch self { case .spades: return "spades" case .hearts: return "hearts" case .diamonds: return "diamonds" case .clubs: return "clubs" } } } let hearts = Suit.hearts let heartsDescription = hearts.simpleDescription() //如果枚举具有原始值,这些值将作为声明的一部分确定,这意味着特定枚举用例的每个实例始终具有相同的原始值。枚举用例的另一个选择是拥有与该用例相关联的值——这些值是在创建实例时确定的,并且对于枚举用例的每个实例,它们可以是不同的。可以将关联值视为枚举用例实例的存储属性。例如,考虑从服务器请求日出和日落时间的情况。服务器要么响应所请求的信息,要么响应错误的描述。
enum ServerResponse { case result(String, String) case failure(String) }
let success = ServerResponse.result("6:00 am", "8:09 pm") let failure = ServerResponse.failure("Out of cheese.")
switch success { case let .result(sunrise, sunset): print("Sunrise is at \(sunrise) and sunset is at \(sunset).") case let .failure(message): print("Failure... \(message)") } // Prints "Sunrise is at 6:00 am and sunset is at 8:09 pm."
//使用struct创建结构。结构支持许多与类相同的行为,包括方法和初始化器。结构和类之间最重要的区别之一是,结构在代码中传递时总是被复制,但是类是通过引用传递的。 struct Card { var rank: Rank var suit: Suit func simpleDescription() -> String { return "The \(rank.simpleDescription()) of \(suit.simpleDescription())" } } let threeOfSpades = Card(rank: .three, suit: .spades) let threeOfSpadesDescription = threeOfSpades.simpleDescription()
//********************Protocols and Extensions //使用协议声明协议。 //使用 mutating 关键字修饰方法是为了能在该方法中修改 struct 或是 enum 的变量,在设计接口的时候,也要考虑到使用者程序的扩展性。所以要多考虑使用mutating来修饰方法。 //注意,在SimpleStructure声明中使用mutating关键字来标记修改结构的方法。SimpleClass的声明不需要任何标记为mutating的方法,因为类上的方法总是可以修改该类。
protocol ExampleProtocol { var simpleDescription: String { get } mutating func adjust() }
|
请发表评论