Upload
tomohiro-kumagai
View
2.985
Download
0
Embed Size (px)
Citation preview
紙版は絶版、電⼦書籍は販売中
:
@available(*, unavailable, renamed: "NewProtocol") typealias PreviousProtocol = NewProtocol
renamed message
@swift3_migration(message: "MESSAGE") func doSomething() { … }
@swift3_migration(renamed: "NewProtocol") typealias PreviousProtocol = NewProtocol
if /* comment */!isValid { ... }
value +/* comment */coefficient
!
+/*
optionalArray/* Comment */?.count
value+/* comment */coefficient
?
+
!
let optionalValue: Int! = 100
var a: Int! var b: Int?
// なぜか ImplicitlyUnwrappedOptional<Int> として認識 print(a.dynamicType)
// こちらは Optional<Int> 型として認識 print(b.dynamicType)
func function(first: Int, second: Int, third: Int) {
}
function(first: 1, second: 10, third: 3)
_
// ラベル名を label にする func function(label value: Int) {
}
func function(_ label value: Int) {
}
func function(@noescape f: () -> Int) {
}
func function(@autoclosure f: () -> Int) {
}
func function(f: @noescape () -> Int) {
}
func function(f: @autoclosure () -> Int) {
}
func function(f: () -> Int) -> (@noescape (Int) -> Int) -> Int {
return { g in g(f()) }
}
func method() -> Int { let g: @noescape (Int) -> Int = { $0 + action() * 2 } return function(g) }
func method() -> Int { func g(_ value: Int) -> Int { return value + action() * 2 } return function(g) }
func function(@noescape f: () -> Int) -> (@noescape (Int) -> Int) -> Int {
return { g in g(f()) }
}
func function(@noescape f: () -> Int) -> @noescape (@noescape (Int) -> Int) -> Int {
return { g in g(f()) }
}
func function() -> (@autoclosure () -> Int) -> Int {
return { (f: @autoclosure () -> Int) in f() } }
// 関数で得た、関数を取る関数に、値をそのまま渡せる let f = function() let result = f(100)
let f: () -> (@autoclosure () -> Int) -> Int = {
return { (f: @autoclosure () -> Int) in f() } }
// 関数を取る関数に、値をそのまま渡せる let result = f(100)
let value: @autoclosure () -> Int = {
return 100 }
func method(inout result: Int) {
}
func method(inOut result: Int) {
}
func method(result: inout Int) {
}
func method(inOut result: inout Int) {
}
func method(inout result: inout Int) {
}
func method(`inout` result: inout Int) {
}
func method(result: inout Int) {
}
func method(inOut result: inout Int) {
}
func function() -> (result: inout Int) -> Void {
return { (result: inout Int) -> Void in
result = 700 } }
func function() -> (result: inout Int) -> Void {
return { $0 = 700 }
}
let f: (inout Int) -> Void = {
return { $0 = 5000 }
}
func freeFall(context: Context)(time: Double) -> Double { return -1 / 2 * time * time * context.gravitationalAcceleration }
func freeFall(context: Context) -> (time: Double) -> Double {
return { time in
-1 / 2 * time * time * context.gravitationalAcceleration
} }
// コンテキストを固定し、その環境を前提に動く関数を定義 let earth = Context(identifier: "Earth", gravitationalAcceleration: 9.80665)
let freeFallOnEarth: (time: Double) -> Double = freeFall(context: earth)
// 汎用関数でスマートに捌く let timeInterval = stride(from: 0.0, through: 100.0, by: 1.0)
let locations = timeInterval.map(freeFallOnEarth)
func function(object: Object)(multiple: Int) -> Int {
let value = object.value return value * multiple }
do { let f = function(Object(100)) print(f(multiple: 5)) }
func function(object:Object) -> (multiple:Int) -> Int {
let value = object.value return { multiple in value * multiple } }
do { let f = function(Object(100)) print(f(multiple: 5)) }
func function(var initial value: Int, count: Int) -> Int {
(1 ..< count).forEach { _ in
value += value }
return value }
func function(var initial value: Int, count: Int) -> Int {
(1 ..< count).forEach { _ in
value += value }
return value }
var value = 10 let answer = function(initial: value, count: 5)
func function(initial value: inout Int, count: Int) {
(1 ..< count).forEach { _ in
value += value }
}
var value = 10
function(initial: &value, count: 5)
func function(var initial value: Int, count: Int) -> Int {
// 即座に value に変更を仕掛けていける (1 ..< count).forEach { _ in
value += value }
return value }
func function(initial value: Int, count: Int) -> Int {
// 編集できるように新しい可変値変数で受け直す var value = value
(1 ..< count).forEach { _ in
value += value }
return value }
func function(initial value: Int, count: Int) -> Int {
return (1 ..< count).reduce(value) { value, _ in
value + value
}
}
func function(let initial value: Int, count: Int) -> Int {
return (1 ..< count).reduce(value) { value, _ in
value + value
}
}
// if var 構文で使用可能 if var value = optionalValue { … }
// while 構文で使用可能 while var value = iterator.next() { … }
// for 構文で使用可能 for var value in values { … }
// switch 構文で使用可能 switch optionalValue { case .some(var value): … case .none: … }
// guard var 構文が使用可能 guard var value = optionalValue else {
fatalError() }
// これ以降で可変値変数 value が使える value += 200