Skip to content

Instantly share code, notes, and snippets.

@takasek
takasek / CodePiece.swift
Created May 13, 2016 08:17
要素がOptionalなArrayに対してextension書きたいかったけど、Optionalはprotocolじゃないから無理か…と悩んでたけど、こうやったらできた! #CodePiece
protocol OptionalType {
associatedtype Wrapped
var optional: Wrapped? { get }
}
extension Optional : OptionalType {
var optional: Wrapped? { return self }
}
extension CollectionType where Generator.Element: OptionalType {
@takasek
takasek / CodePiece.swift
Created May 14, 2016 12:58
整数計算時に桁あふれが実行時エラーを引き起こす件と、IntegerArithmeticType.xxxWithOverflowの使い方について。 #CodePiece
// 整数計算で桁あふれが起こると実行時エラーになる
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)
@takasek
takasek / CodePiece.swift
Created May 14, 2016 13:01
Swiftの浮動小数は実は安全なゼロ除算が可能(infが返る)。しかしIntは実行時エラーになる。この実行時エラーはdivideWithOverflowでも防げない。 #CodePiece
// Doubleは実はゼロ除算可能(infが返る)
10/0.0 // inf
// Intではゼロ除算すると実行時エラー
//10/0 // error: division by zero
// IntegerArithmeticType.divideWithOverflowを使っても、ゼロ除算での実行時エラーは防げない
//UInt8.divideWithOverflow(254, 0) // error: division by zero
@takasek
takasek / CodePiece.swift
Created May 22, 2016 12:56
Boolのextensionで `func toggle()` 実装してみた。immutableなBoolに対してはコンパイルエラーになるし期待以上に使い勝手よさそう…! #CodePiece
extension Bool {
mutating func toggle() {
self = !self
}
}
var bool = true // true
bool.toggle() // false
let immutableBool = true
@takasek
takasek / CodePiece.swift
Created May 25, 2016 23:23
https://github.com/takasek/ActionClosurable 0.2.0でUIGestureRecognizerがより便利になりました。 #CodePiece
// UIControl
button.on(.TouchDown) {
$0.backgroundColor = UIColor.redColor()
}
button.on(.TouchUpOutside) {
$0.backgroundColor = UIColor.whiteColor()
}
// UIButton
button.onTap {
$0.enabled = false
@takasek
takasek / CodePiece.swift
Created June 9, 2016 06:33
えっマジで…ジェネリクス型ってキャストできないの…あまりにつらすぎる… #CodePiece
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
@takasek
takasek / CodePiece.swift
Created June 30, 2016 10:37
違う、Measurementはstruct、UnitはNSObjectか #wwdc16_meetup #CodePiece
//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)
}
@takasek
takasek / CodePiece.swift
Created July 9, 2016 05:47
タプルを使った漏れのない値設定。(inspired by http://twitter.com/eduraaa/status/751621270790246400 ) #CodePiece
(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:
@takasek
takasek / CodePiece.swift
Created July 9, 2016 06:08
なるほど、タプルのラベル順が前後しても問題ない(有効なユースケースが思いつかないけどw)のと、正しいコンポーネントに正しい値が入ることを目で確認しやすいのは利点だなあ。 #CodePiece
(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")
}
}()
@takasek
takasek / CodePiece.swift
Created July 9, 2016 06:52
りずさんのhttps://t.co/Yq0bfT8jdx をinfix operator使わず、普通の関数で書いてみた。結果は同じ。引数(タプルではない)にタプルを渡せる #CodePiece
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)