The Swift Programming Language 2.1 Examples
源码在 GitHub:https://github.com/gewill/The-Swift-Programming-Language-2.1-Examples
Playground ->
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
import UIKit
struct FiexdLengthRange { var firstValue: Int let length: Int }
var x = FiexdLengthRange(firstValue: 1, length: 3) x.firstValue = 4
let y = FiexdLengthRange(firstValue: 4, length: 2)
class DataImporter { var firstName = "data.txt" }
class DataManager { lazy var importer = DataImporter() var data = [String]() }
let manager = DataManager() manager.data.append("New") manager.data.append("Two")
struct Point { var x = 0.0, y = 0.0 }
struct Size { var width = 0.0, height = 0.0 }
struct Rect { var origin = Point() var size = Size() var center: Point { mutating get { let centerX = origin.x + size.width / 2 let centerY = origin.y + size.height / 2 return Point(x: centerX, y: centerY) } set(newCenter) { origin.x = newCenter.x - size.width / 2 origin.y = newCenter.y - size.height / 2 } } }
var rect0 = Rect(origin: Point(), size: Size(width: 3.0, height: 4.0)) rect0.center rect0.center = Point(x: 50, y: 50) rect0.center
struct Cuboid { var width = 0.0, height = 0.0, depth = 0.0 var volume: Double { return width * height * depth } } Cuboid(width: 4.0, height: 5.0, depth: 2.0).volume
class StepCounter { var totalSteps: Int = 0 { willSet(newTotolSteps) { if totalSteps != newTotolSteps { print("The totalSteps will change value to \(totalSteps).") } } didSet { if totalSteps > oldValue { print("The totalSteps add \(totalSteps - oldValue) steps. ") } } } }
StepCounter().totalSteps = -33 var stepCounter = StepCounter() stepCounter.totalSteps = 33 stepCounter.totalSteps = 11
struct SomeStructure { static var storedTypedProperty = "some value" static var computedTypedProperty: Int { return 1 } }
enum SomeEnumeration { static var storedTypedProperty = "some value" static var computedTypedProperty: Int { return 6 } } class SomeClass { static var storedTypedProperty = "some value" static var computedTypedProperty: Int { return 7 }
class var overrideableComputedTypeProperty: Int { return 109 } }
struct AudioChannel { static let threshoudLevel = 10 static var maxInputLevelForAllChannels = 0 var currentLevel: Int = 0 { didSet { if currentLevel > AudioChannel.threshoudLevel { currentLevel = AudioChannel.threshoudLevel } if currentLevel > AudioChannel.maxInputLevelForAllChannels { AudioChannel.maxInputLevelForAllChannels = currentLevel } } } }
var leftChannel = AudioChannel() var rightChannel = AudioChannel()
leftChannel.currentLevel = 7 print(leftChannel.currentLevel) print(AudioChannel.maxInputLevelForAllChannels)
rightChannel.currentLevel = 11 print(rightChannel.currentLevel) print(AudioChannel.maxInputLevelForAllChannels)
|