9
swift run
% swiftrun
Building for debugging...
[3/3] Linking hello
Build complete! (0.96s)
Hello, World!
% file ./.build/x86_64-apple-macosx/debug/hello
./.build/x86_64-apple-macosx/debug/hello: Mach-O 64-bit executable x86_64
10.
10
Swift は Pythonに似ている(こともある!)
# Python
for animal in ["cat", "dog", "snake"]:
print(animal)
// Swift
for animal in ["cat", "dog", "snake"] {
print(animal)
}
12
タプルの代入
# Python
dog, cat= ("dog", "cat")
print(dog, cat)
// Swift
var (dog, cat) = ("dog", "cat")
print(dog, cat)
13.
13
let と var
//JavaScript の分割代入
let [dog, cat] = [“dog”, “cat”] // 変数(ブロックスコープ)
var [dog, cat] = [“dog”, “cat”] // 変数(グローバル)
// 定数は const
// Swift
let は定数
var は変数
14.
14
Swiftのタプルはシーケンスではない
var animals =("dog", "cat")
print(animals)
print(animals.0)
print(animals.1)
// for animal in animals {
// print(animal)
// }
// エラーになる
15.
15
Pythonの正規表現
import re
s ="My name is Taylor and I'm 26 years old."
m = re.search("My name is (.+?) and I'm (d+) years old.", s)
if m:
print(f"Name:{m.group(1)}")
print(f"Age:{m.group(2)}")
16.
16
Swiftの正規表現
let s ="My name is Taylor and I'm 26 years old."
let m = /My name is (.+?) and I'm (d+) years old./
if let g = try? m.wholeMatch(in: s) {
print("Name: (g.1)")
print("Age: (g.2)")
}
// since Swift 5.7