Skip to content

Instantly share code, notes, and snippets.

View tarunon's full-sized avatar
🐌

tarunon tarunon

🐌
View GitHub Profile
struct User<T> { ... } // ←こういうものをConformしたい
extension Array where User<T> { ... } // ←これはまだできない
// ここからWorkaround
protocol UserProtocol { associatedtype T } // ←まずこれを作って
extension User: UserProtocol {} // Conformして
public struct _ModelX<_ModelN: Codable>: Codable {
public var model0: _ModelN
public var model1: Bool?
public var model2: Bool?
public var model3: Bool?
public var model4: Bool?
public var model5: Bool?
public var model6: Bool?
public var model7: Bool?
public var model8: Bool?

indexPath

  • Aの下にB、というような相対的な配置ではなく絶対座標による指定。ViewOnlyでレイアウトを組むことができず、indexPathを設定するためのロジックが必須になる
  • ↑データ群からindexPathを一意に変換する(場合によっては双方向も)必要があり、ここがFatになりやすい
  • StaticTableViewは避けれるが、IBがFatになりやすい。Storyboardのみ。

Cellへのアクセス

  • 安全なアクセスは生成時に限定されている、ReuseもあるのでデータをCellで保持することが出来ない。もっともMVVMだとVMにデータを保持するので元からそういう作りであれば問題にはならないか。ただしFatVMになりやすいと感じている。
  • CellのイベントハンドラをVCに作るときにindexPathを取り出して…など結構煩雑になりがち。
  • tableViewCellForIndexPathがFatになりやすい。
  • StaticTableViewは避けれるが
@tarunon
tarunon / gist:6da5e305f49e67757dfd353603baa8c9
Last active March 15, 2018 04:51
Protocol+Class+Erasure.swift
class Animal: AnimalConvertible {
func bark() -> String { fatalError() }
func asAnimal() -> Animal {
return self
}
}
protocol AnimalConvertible {
func bark() -> String
func asAnimal() -> Animal
/// とりあえず各ケースのOptional変換を作っておく。ここはPrivateでも良い。
extension Enum2Convertible {
func ifCase0() -> Case1? {
switch self.asEnum() {
case .case0(let x): return x
default: return nil
}
}
func ifCase1() -> Case1? {
class Animal {}
class Cat: Animal {}
// Rule A
// Cat is subtype of Animal
do {
let cat: Cat = Cat()
let animal: Animal = cat
}
import Foundation
// Make 5 pattern base types
class A {}
protocol B {}
protocol C: class {}
@objc protocol D {}
protocol E: NSObjectProtocol {}
// 多変数の型パラ持ちのprotocolでtype-erasureを書く時に、共変性をサポートしたいが
// 素直なtype-erasureでは引数にバラバラにpropertyを渡すことでしか共変な型消しをinitできなかった。
class Animal {}
class Cat: Animal {}
protocol A {
associatedtype X
var x: X { get }
protocol Injectable {
associatedtype Dependency = Void
func inject(_ dependency: Dependency)
}
extension Injectable where Dependency == Void {
func inject(_ depenency: Dependency) {
}
@tarunon
tarunon / Undefined.swift
Created March 24, 2017 05:27
lazy var fatalError is a solution that instead of ImplicitlyUnwrappedOptional
func undefined<T>(_ funcName: String=#function) -> T {
fatalError("\(funcName) is undefined.")
}
class A {
lazy var parameter: Int = undefined()
}
let a = A()
a.parameter = 100