Skip to content

Instantly share code, notes, and snippets.

View ryotapoi's full-sized avatar

Ryota Kurosaki ryotapoi

View GitHub Profile
@ryotapoi
ryotapoi / CRC32.swift
Last active November 9, 2020 03:58
Swiftで高速にCRC32を求める。
import Foundation
import zlib
struct CRC32: Equatable, ExpressibleByIntegerLiteral, CustomStringConvertible {
typealias IntegerLiteralType = UInt32
let value: UInt32
var description: String { String(format: "%08X", value) }
init(data: Data) {
@ryotapoi
ryotapoi / SequencialPublisher.swift
Created November 6, 2020 04:29
Publisherの配列を先頭からひとつずつ順次実行する。
import Foundation
import Combine
extension Combine.Publishers {
struct Sequencial<Sequence, Output, Failure>: Publisher
where
Sequence: Swift.Sequence,
Sequence.Element: Publisher,
Sequence.Element.Output == Output,
Sequence.Element.Failure == Failure
@ryotapoi
ryotapoi / FileManagerExtensions.swift
Created November 6, 2020 04:26
FileManagerによるファイル操作でCombineを使う。
import Foundation
import Combine
extension FileManager {
struct RemoveItemPublisher: Publisher {
typealias Output = Void
typealias Failure = Error
var url: URL
var fileManager: FileManager
@ryotapoi
ryotapoi / DateComponents.swift
Created November 2, 2020 09:30
経過時間などを表す mm:ss 形式のフォーマット
import Foundation
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .positional
formatter.allowedUnits = [.minute, .second]
formatter.zeroFormattingBehavior = .pad
formatter.string(from: 61)
@ryotapoi
ryotapoi / command.sh
Created August 22, 2020 15:02
無圧縮zipをコマンドで作る
zip -0 -r archive.zip source…
@ryotapoi
ryotapoi / objc.m
Created August 22, 2020 14:42
未使用の戻り値で警告を抑制する
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-value"
c.view;
#pragma clang diagnostic pop
@ryotapoi
ryotapoi / URLComponentsCompatible_playground.swift
Created August 2, 2020 09:29
Custom URL Schemeをstructにマッピングする。
import Foundation
// URLQueryValue
public enum URLQueryValueCompatibleError: Error {
case none // nil
case empty // isEmpty
case format // 期待した書式ではない
}
import UIKit
import Combine
// MARK: - UserDefaults
public protocol UserDefaultsProtocol: NSObject {
func value<Value: UserDefaultCompatible>(type: Value.Type, forKey key: String, default defaultValue: Value) -> Value
func setValue<Value: UserDefaultCompatible>(_ value: Value, forKey key: String)
}
@ryotapoi
ryotapoi / CurrentValueSubjectでUserDefaultsを更新するplayground.swift
Last active July 12, 2020 16:45
CurrentValueSubjectを変更するとUserDefaultsにも保存されるサンプル
import UIKit
import Combine
// これだけでは使いにくい。
// * 色々な型への対応
// * Property WrapperのprojectedValueとしてCurrentValueSubjectを返す
// とすれば使いやすくなるはず。
class Box {
var cancelable: AnyCancellable?
@ryotapoi
ryotapoi / MySubject.swift
Last active July 12, 2020 17:07
Combine.Subjectを実装してCurrentValueSubjectのクローンを作る
import Combine
class MySubject<Output, Failure : Error> : Combine.Subject {
var value: Output {
didSet {
// 完了していなければ値を通知する
guard completion == nil else { return }
subscriptions.forEach { $0.receive(value) }
}
}