Skip to content

Instantly share code, notes, and snippets.

@takasek
takasek / CodePiece.swift
Last active February 9, 2019 06:03
リフレクションでEnumのcase名を取得できないかと思ったけど断念。値つきなら可能みたいだけど、プレーンに取りたいんやー 諦めてEnum:Stringにするしかないか… #CodePiece
enum Hoge {
case A
case B()
case C(Int)
}
let a = Mirror(reflecting: Hoge.A) // Mirror for Hoge
a.children.first // nil
a.displayStyle // Enum
a.description // "Mirror for Hoge"
@takasek
takasek / CodePiece.swift
Created April 14, 2016 07:55
ジェネリック型を取り扱うカリー化関数を作りたいんだけど、コンパイルエラーでうまくいかない… #CodePiece
// indexを先に与えてカリー化した関数を返し、そこにcollectionを与えると要素が返ってくる関数
func getObject<C: CollectionType, I: ForwardIndexType where C.Index == I>(index: I)
-> C -> C.Generator.Element {
return { collection in
return collection[index]
}
}
// 一気に呼べばうまくいく
getObject(2)(["0ばんめ","1ばんめ","2ばんめ"]) // "2ばんめ"
@takasek
takasek / CodePiece.swift
Created April 14, 2016 08:28
カリー化関数自体をジェネリクスとして扱いたいんだけど無理だろうか #CodePiece
// Dictionaryを束縛するwhereの書き方がわからず、動かないコードですが、雰囲気は伝わるかな
func getValue<D: Dictionary, K: Hashable, V where K == D.Key, V == D.Value>(key: K)
-> D -> V {
return { dict in
return dict[key]
}
}
let gv = getValue("k") as [String: ❓❓❓]
@takasek
takasek / CodePiece.swift
Created April 14, 2016 09:00
関数だけでは賄えず、structを作ることになってしまった…でも、indexは束縛しつつ、Elementの型の判断を遅らせることができた! #CodePiece
struct ObjectSender<I: ForwardIndexType> {
let index: I
func element<C: CollectionType where C.Index == I>
(of collection: C) -> C.Generator.Element {
return collection[index]
}
}
let g2 = ObjectSender(index: 2)
g2.element(of: ["0ばんめ","1ばんめ","2ばんめ"])
@takasek
takasek / CodePiece.swift
Created April 14, 2016 09:26
こんなかんじです。 R.swiftべんりだよ。 #CodePiece
import UIKit
import Rswift
struct Dequeuer {
private weak var tableView: UITableView!
private let indexPath: NSIndexPath
func dequeue<Identifier: ReuseIdentifierType where Identifier.ReusableType: UITableViewCell>(identifier: Identifier) -> Identifier.ReusableType {
return tableView.dequeueReusableCellWithIdentifier(identifier.identifier, forIndexPath: indexPath) as! Identifier.ReusableType
}
}
@takasek
takasek / CodePiece.swift
Created April 18, 2016 03:08
気になるビルドエラー。おいおい、まるで「swiftのclassはprotocol typeである」みたいな言い草じゃあないか? #CodePiece
protocol P {}
class C {}
extension P0 where Self: C {} // classだとOK
struct S {}
extension P0 where Self: S {} // structだとNG: type 'Self' constrained to non-protocol type 'S'
@takasek
takasek / CodePiece.swift
Created April 18, 2016 03:14
気になるビルドエラー。おいおい、まるで「swiftのclassはprotocol typeである」みたいな言い草じゃあないか? #CodePiece
protocol P {}
class C {}
extension P where Self: C {} // classだとOK
struct S {}
extension P where Self: S {} // structだとNG:
// type 'Self' constrained to non-protocol type 'S'
@takasek
takasek / CodePiece.swift
Created April 22, 2016 09:05
なるほど、NSObjectを継承しないclassではデフォルトではEquatableに適合していないので、collectionに入れるとindexOfが使えない。 #CodePiece
import Foundation
class O: NSObject {}
// NSObjectのEquatableは isEqual: によって保証されている。
// NSObjectにおけるisEqual:のデフォルトの実装は、単純にポインタの等価性を確認するだけ。
let o = O()
let os = [o]
os.indexOf(o) // OK
@takasek
takasek / CodePiece.swift
Created April 22, 2016 09:16
「このクラスはポインタの等価性で同一性を判定できます」と示すプロトコルを用意しておいてもよさそうだと思った。実装はこんな感じかな。 #CodePiece
protocol ObjectiveEquatable: class, Equatable {}
func == <T: ObjectiveEquatable>(lhs: T, rhs: T) -> Bool {
return lhs === rhs
}
class C: ObjectiveEquatable {}
let c = C()
let cs = [c]
cs.indexOf(c)
@takasek
takasek / CodePiece.swift
Created April 24, 2016 10:26
FoundationなしだとStringのreplaceすらできないよね 試しに自前実装してみたけど辛すぎる Cocoa外でSwift書く人はこんな風に書いてるの…!? #CodePiece
extension String {
func replaceWithoutFoundation(target: String, replacement: String) -> String {
var stack = [Character]()
return String(self.characters.generate().flatMap { c -> [Character] in
stack.append(c)
if !target.hasPrefix(String(stack)) {
//targetと相違したのでstackを放出
let result = stack
stack.removeAll()