Skip to content

Instantly share code, notes, and snippets.

View ryotapoi's full-sized avatar

Ryota Kurosaki ryotapoi

View GitHub Profile
@ryotapoi
ryotapoi / UserDefaultPropertyWrapper_Playground.swift
Last active May 22, 2021 12:19
UserDefaultPropertyWrapper for Playground
import UIKit
// UserDefaults
extension UserDefaults {
public func value<Value : UserDefaultCompatible>(type: Value.Type = Value.self, forKey key: String, default defaultValue: Value) -> Value {
guard let object = object(forKey: key) else { return defaultValue }
return Value(userDefaultObject: object) ?? defaultValue
}
public func setValue<Value : UserDefaultCompatible>(_ value: Value, forKey key: String) {
@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 / MultipartFormData.swift
Last active November 2, 2020 08:58
multipart/form-dataを送るためのヘルパー for swift
import Foundation
public struct MIMEType: RawRepresentable, CustomStringConvertible {
public var rawValue: String
public var description: String { rawValue }
public init?(rawValue: String) {
self.rawValue = rawValue
}
@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)
}