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
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" |
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
// 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ばんめ" |
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
// 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: ❓❓❓] |
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 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ばんめ"]) |
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
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 | |
} | |
} |
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 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' |
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 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' |
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
import Foundation | |
class O: NSObject {} | |
// NSObjectのEquatableは isEqual: によって保証されている。 | |
// NSObjectにおけるisEqual:のデフォルトの実装は、単純にポインタの等価性を確認するだけ。 | |
let o = O() | |
let os = [o] | |
os.indexOf(o) // OK |
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 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) |
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 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() |