Files
2021-12-14 09:36:26 +08:00

195 lines
3.3 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import UIKit
var greeting = "Hello, playground"
//: [Previous](@previous)
//: ##
//: ###
/*:
a...b : a <= <= b
*/
let names = ["Anna", "Alex", "Brian", "Jack"]
for i in 0...3 {
print(names[i])
}
let a = 1
var b = 3
for i in a...b {
print(names[i])
}
let range = 1...3
for i in range {
print(names[i])
}
// iletvar
for var i in 1...3 {
// i
i += 5
print(i)
}
// ,_
for _ in 1...3 {
print("123456")
}
//: ###
/*:
a..<b : a <= < b
*/
//: ###
/*:
a... : a <=
...b : <= b
*/
//: ###
// ClosedRange
let range1: ClosedRange<Int> = 1...3
// Range
let range2: Range<Int> = 1..<3
// PartialRangeThrough
let range3: PartialRangeThrough<Int> = ...5
//: ###
// 使stride
for tickMark in stride(from: 4, to: 11, by: 2) {
print(tickMark)
}
//: ## switch
/*:
switch
- switch
- default
- switch CharacterString
- switch
*/
// break穿
var num = 1
switch num {
case 1:
print("number is 1")
case 2:
print("number is 2")
default:
print("number is other")
}
// 使fallthrough穿
var number = 1
switch number {
case 1:
print("number is 1")
fallthrough
case 2:
print("number is 2")
default:
print("number is other")
}
//: ####
let count = 62
switch count {
case 0:
print("none")
case 1..<5:
print ("a few")
case 5..<12:
print("several")
case 12..<100:
print("dozens of")
case 100..<1000:
print("hundreds of")
default:
print("many")
} // dozens of
//: ####
// 使线_
let point = (1, 1)
switch point {
case (0, 0):
print("the origin")
case (_, 0):
print ("on the x-axis")
case (0, _):
print("on the y-axis")
case (-2...2, -2...2):
print("inside the box")
default:
print( "outside of the box")
}
//: ###
// let var
let point1 = (2, 0)
switch point1 {
case (let x, 0):
print("on the x-axis with a x value of \(x)")
case (0, let y):
print("on the y-axis with a y value of \(y)")
case let(x, y):
print("somewhere else at (\(x), \(y))")
}
//: ### where
// 1. case
let point2 = (1, -1)
switch point2 {
case let(x, y) where x == y:
print("on the line x == y")
case let(x, y) where x == -y:
print("on the line x == -y")
case let(x, y):
print("somewhere else at (\(x), \(y))")
}
//2.
var numbers = [10, 20, 30, -10, -30]
var sum = 0
for num in numbers where num > 0 {
sum += num;
}
print(sum)
//: ###
//
// :
outer: for i in 1...4 {
for k in 1...4 {
if k == 3 {
continue outer
}
if i == 3 {
break outer
}
print("i == \(i), k == \(k)")
}
}
//: [Next](@next)