This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol OptionalType { | |
associatedtype Wrapped | |
var optional: Wrapped? { get } | |
} | |
extension Optional : OptionalType { | |
var optional: Wrapped? { return self } | |
} | |
extension CollectionType where Generator.Element: OptionalType { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 整数計算で桁あふれが起こると実行時エラーになる | |
UInt8(254) + 1 // 255 | |
//UInt8(255) + 1 // error: arithmetic operation '255 + 1' (on type 'UInt8') results in an overflow | |
// static func IntegerArithmeticType.xxxWithOverflowは、桁あふれの可能性のある計算で使える | |
UInt8.addWithOverflow(254, 1) // (.0 255, .1 false) | |
UInt8.addWithOverflow(255, 1) // (.0 0, .1 true) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Doubleは実はゼロ除算可能(infが返る) | |
10/0.0 // inf | |
// Intではゼロ除算すると実行時エラー | |
//10/0 // error: division by zero | |
// IntegerArithmeticType.divideWithOverflowを使っても、ゼロ除算での実行時エラーは防げない | |
//UInt8.divideWithOverflow(254, 0) // error: division by zero |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Bool { | |
mutating func toggle() { | |
self = !self | |
} | |
} | |
var bool = true // true | |
bool.toggle() // false | |
let immutableBool = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// UIControl | |
button.on(.TouchDown) { | |
$0.backgroundColor = UIColor.redColor() | |
} | |
button.on(.TouchUpOutside) { | |
$0.backgroundColor = UIColor.whiteColor() | |
} | |
// UIButton | |
button.onTap { | |
$0.enabled = false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Hoge<T> { | |
let v: T | |
} | |
protocol P {} | |
extension String: P {} | |
let s = Hoge(v: "a") | |
let sp = s as? Hoge<P> // Cast from 'Hoge<String>' to unrelated type 'Hoge<P>' always fails |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//cf. http://yannickloriot.com/2016/06/measurements-and-units-ios/ | |
public struct Measurement<UnitType: Unit>: Comparable, Equatable { | |
public init(value: Double, unit: UnitType) | |
} | |
public class Unit: NSObject, NSCopying { | |
public init(symbol: String) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(label.text, button.selected, slider.value) = { | |
() -> (text: String?, selected: Bool, value: Float) in | |
switch condition { | |
case .one: | |
return (text: "1", selected: true, value: 0.1) | |
case .two: | |
return (text: 2, selected: false, value: 0.2) | |
//error: | |
// cannot convert return expression of type 'Int' to return type 'String?' | |
case .three: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(c: labelC.text, a: labelA.text, b: labelB.text) = { | |
switch condition { | |
case .one: | |
return (a: "1", b: "11", c: "111") | |
case .two: | |
return (b: "22", c: "222", a: "2") | |
case .three: | |
return (a: "3", c: "333", b: "33") | |
} | |
}() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func apply <A, B>(f : A -> B, a : A) -> B { | |
return f(a) | |
} | |
func p(a a: Int, b: Int) { | |
print("a: \(a)") | |
print("b: \(b)") | |
} | |
let tuple: (b: Int, a: Int) = (b: 2, a: 1) |