swift_playground
1.0.0
Swift Playground:在 Playground 中探索並評估 Swift 程式碼(複製貼上)
這些例子來自:
https://developer.apple.com/library/ios/recipes/xcode_help-source_editor/chapters/ExploringandEvaluatingSwiftCodeinaPlayground.html
在上面的頁面上,程式碼範例是圖像,這非常煩人。我在這裡提供它們,以便您可以透過複製和貼上來節省時間:
// Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
let triple: Int -> Int = {
(number: Int) in
let result = 3 * number
number
return number
}
triple(3)
let listOfNumbers = 1...5
var sum = 0
for atNum in listOfNumbers {
sum += atNum
}
sum
var j = 2
for var i = 0; i < 5; ++i {
j += j * i
}
j
////////
// let j = 3 // ERROR: Invalid redeclaration of 'j'
////////
let size = (20, 40)
switch size {
case let (width, height) where width == height:
println("square with sides(width)")
width
height
case (1...10, 1...10):
println("")
case let (width, height):
println("rectangle with width (width) and height (height)")
width
height
}
////////
let frame = CGRect(x: 0, y: 0, width: 150 , height: 150)
let customView = UIView(frame: frame) // CheckBox(frame: frame) // Switched to a UIView so the example would work
customView.backgroundColor = UIColor.blueColor()
customView.tintColor = UIColor.orangeColor()
//customView.isChecked = true
////////
j = 2
for var i = 0; i < 5; ++i {
j += j * i
}
j
import XCPlayground
for (_, color) in enumerate([UIColor.orangeColor(), UIColor.greenColor()]) {
customView.backgroundColor = color
XCPShowView("customView", customView)
}
* Swift 是一種專有語言。蘋果世界是一個志工監獄。