Skip to content

Instantly share code, notes, and snippets.

@takasek
takasek / AtCoderTemplate.swift
Last active Oct 30, 2020
SwiftでAtCoderに挑戦するためのPlayground用テンプレート
View AtCoderTemplate.swift
import Foundation
struct Example {
let input: String
let expectation: String
}
// テストケース列挙
let examples: [(String, Example)] = [
("1", Example(
input: """
@takasek
takasek / PolymorphicDecodable.swift
Last active Jul 18, 2020
「派生型プロパティを Decodable で扱う」 https://hitorigoto.zumuya.com/200716_decodableProtocolProperty にインスパイヤされたコード。いじってたらPropertyWrapperがなくなってしまった…
View PolymorphicDecodable.swift
// 汎用コード
struct CustomCodingKey: CodingKey, ExpressibleByStringLiteral {
let stringValue: String
let intValue: Int? = nil
init?(stringValue: String) { self.stringValue = stringValue }
init?(intValue: Int) { return nil }
init(stringLiteral value: String) { stringValue = value }
init(_ value: String) { stringValue = value }
}
@takasek
takasek / CodePiece.rb
Created Jul 11, 2020
さっきの https://anoqode.com/question?id=9dd1d9f9-bba9-4cde-b331-1ab78b155b01 ですけど、RawRepresentableなstructを噛ませておくと、失敗した文字列も失わずに済むので好きです #swiftzoomin #CodePiece
View CodePiece.rb
let jsonData = """
[
"https://www.apple.com",
"https://swift.org/blog/",
"https://github.com/apple/swift/releases/tag/swift 5.2.4-RELEASE"
]
"""
.data(using: .utf8)!
struct SafeURL: RawRepresentable, Decodable {
let rawValue: String
@takasek
takasek / EntityID.swift
Last active Oct 15, 2019
「EntityはIDを持ってるよね」をprotocolで表現し、Entity.IDを定義不要にする
View EntityID.swift
protocol Entity {
associatedtype RawIDValue: Hashable
typealias ID = EntityID<Self>
}
struct EntityID<E: Entity>: RawRepresentable, Hashable {
let rawValue: E.RawIDValue
}
extension EntityID {
init(_ value: E.RawIDValue) {
@takasek
takasek / CodePiece.swift
Created Jun 2, 2019
静的型付け言語であるSwiftもダックタイピングはできるよ(オブジェクト指向(メッセージ指向)言語であるObj-Cとcompatibleだから)というコード #CodePiece
View CodePiece.swift
class Cat: NSObject {
@objc func bark() { print("meow") }
func hoge() {}
}
class Dog: NSObject {
@objc func bark() { print("bow-wow") }
func hoge() {}
}
let cat = Cat()
@takasek
takasek / CodePiece.swift
Created Mar 21, 2019
`KeyPath` for immutable property works differently between Swift4.2 / 5 #CodePiece #tryswiftconf
View CodePiece.swift
func replace<T>(_ root: inout T, keyPath: KeyPath<T, String>) {
if let k = keyPath as? WritableKeyPath<T, String> {
print("writable:", keyPath)
root[keyPath: k] += " 🖍"
} else {
print("not writable:", keyPath)
}
}
struct S {
let a = "let"
@takasek
takasek / iOS_architecture_03.swift
Created Feb 11, 2019
iOSアプリ設計パターン入門 第3章サンプルコード
View iOS_architecture_03.swift
protocol MessageSenderDelegate {
func stateの変化を伝える()
}
protocol Message {
}
protocol MessageInput {
associatedtype Payload
func validate() throws -> Payload
}
@takasek
takasek / CodingKeyToSnake.swift
Last active Aug 5, 2020
CodingKeyをJSONのkeyに寄せて書くことができた!
View CodingKeyToSnake.swift
// https://github.com/apple/swift/blob/b0f5815d2b003df628b1bcfe94681fec489c9492/stdlib/public/Darwin/Foundation/JSONEncoder.swift#L153
func _convertToSnakeCase(_ stringKey: String) -> String {
guard !stringKey.isEmpty else { return stringKey }
var words : [Range<String.Index>] = []
// The general idea of this algorithm is to split words on transition from lower to upper case, then on transition of >1 upper case characters to lowercase
//
// myProperty -> my_property
// myURLProperty -> my_url_property
//
@takasek
takasek / LoginUseCaseTests.swift
Created Sep 28, 2018
RxTest, TestSchedulerによるテスト例
View LoginUseCaseTests.swift
import RxSwift
import RxCocoa
import RxTest
struct LoginData: Equatable {
let id: Int
}
protocol LoginRepository {
func post(data: LoginData) -> Single<Account>
}
@takasek
takasek / CodePiece.swift
Created Apr 17, 2018
ここらへんのジェネリクスのネストの挙動の理解、だいぶ昔のまま止まってたかも #CodePiece
View CodePiece.swift
struct A<T> {
let t: T
struct S1 {
let t: T
}
struct S2<T> {
let t: T
}
func xxx() -> S1 {
return S1(t: t)