Skip to content

Instantly share code, notes, and snippets.

Avatar

はるふ ha1f

View GitHub Profile
View WeakObject.swift
import Foundation
final class WeakObject<T: AnyObject> {
weak var _object: T?
init(_ object: T) {
_object = object
}
}
View String+hexRepresentation.swift
private func _hexString<I: BinaryInteger>(_ value: I) -> String {
"0x" + String(value, radix: 16)
}
extension Character {
func hexRepresentation() -> String {
unicodeScalars
.map { _hexString($0.value) }
.joined(separator: ", ")
}
View AVAsset+Extension.swift
extension AVAsset {
private func _generator(size: CGSize) -> AVAssetImageGenerator {
let generator = AVAssetImageGenerator(asset: self)
generator.maximumSize = size
generator.appliesPreferredTrackTransform = true
return generator
}
func extractUIImageAsync(size: CGSize) -> Single<UIImage?> {
let generator = _generator(size: size)
View Decodable+Extension.swift
public extension KeyedDecodingContainerProtocol {
func decode<T: Decodable>(forKey key: Key) throws -> T {
try decode(T.self, forKey: key)
}
func decodeSafe<T: Decodable>(forKey key: Key, defaultValue: T) -> T {
(try? decode(T.self, forKey: key)) ?? defaultValue
}
}
View EmptyDecoded.swift
import Foundation
public protocol EmptyInitializable {
init()
}
extension Int: EmptyInitializable {}
extension String: EmptyInitializable {}
extension Array: EmptyInitializable {}
extension Bool: EmptyInitializable {}
View UIKit+lerp.swift
//
// UIKit+lerp.swift
// ha1f
//
extension CGRect {
/// 線形補間した中間矩形を返す
/// - parameter otherRect: 目指す先の矩形
/// - parameter rate: 0-1の間の値。0なら自分と、1ならotherRectと同じ。
func lerp(_ otherRect: CGRect, rate: CGFloat) -> CGRect {
View CopyableLabel.swift
import UIKIt
class CopyableLabel: UILabel {
override init(frame: CGRect) {
super.init(frame: frame)
_commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
View UIImagePickerController+Rx.swift
extension Reactive where Base: UIImagePickerController {
static func createAndPresent(from parent: UIViewController?, animated: Bool = true, configureImagePicker: @escaping (UIImagePickerController) throws -> Void = { x in }) -> Observable<[UIImagePickerController.InfoKey: Any]> {
return Observable.create { [weak parent] observer in
let imagePicker = UIImagePickerController()
let dismissDisposable = imagePicker.rx
.didCancel
.subscribe(onNext: { [weak imagePicker] _ in
guard let imagePicker = imagePicker else {
return
}
View URLBuilder.swift
/// JavaのStringBuilder的なインターフェースを想定
final class URLBuilder {
private var components: URLComponents
init?(string: String) {
guard let components = URLComponents(string: string) else {
return nil
}
self.components = components
}
@ha1f
ha1f / Parser.swift
Created Oct 8, 2020
うごかん・・・
View Parser.swift
import Foundation
struct Parsed<Token> {
var token: Token
var remaining: String
init(_ token: Token, remaining: String) {
self.token = token
self.remaining = remaining
}