Skip to content

Instantly share code, notes, and snippets.

@sakiyamaK
sakiyamaK / string_int_unicode_extension.swift
Created December 9, 2023 09:48
StringとInt型でunicodeの変換
extension String {
var unicodeIntList: [Int] {
self.compactMap {
Int(UnicodeScalar(String($0))?.value ?? 0)
}
}
}
extension Int {
var unicodeString: String {
@sakiyamaK
sakiyamaK / ScrollNavigationBarController.swift
Last active August 10, 2023 05:10
collectionview(scrollview)のスクロールに合わせてnavigationbarもスクロールする
// scrollviewの動きに合わせてNavigationBar(もしくは画面上部にあるカスタムビュー)を隠す処理をするクラス
final class ScrollHideNavigationBarAction: NSObject {
private static let invalidOffset: CGPoint = .init(x: -1, y: -1)
// デフォルトのOffset
private var initContentOffset: CGPoint
// レイアウトを組み終わったかどうか判断するフラグ
private var isViewDidLayout: Bool
// scrollStartContentOffsetの初期値 ユーザの操作では絶対出ない座標にする
private var defScrollStartContentOffset: CGPoint
@sakiyamaK
sakiyamaK / pdf_ebook.sh
Created June 24, 2023 09:44
inputフォルダにあるpdfをebook向けに読める範囲内で可能な限り容量小さく圧縮してoutputフォルダに出力するスクリプト
for file in input/*.pdf; do gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile="output/$(basename "$file")" "$file"; done
@sakiyamaK
sakiyamaK / SampleExpressibleByXXX.swift
Created July 31, 2022 09:14
SampleExpressibleByXXX.swift
//https://qiita.com/taisuke_h/items/f6940ba087404d88b138
struct UserId {
let value: String
}
extension UserId: ExpressibleByStringLiteral {
init(stringLiteral value: String) {
self = UserId(value: value)
}
@sakiyamaK
sakiyamaK / SamplePropertyWrapper+Rx.swift
Last active August 1, 2022 16:23
PropertyWrapperを使ってRxSwiftをラップ
import UIKit
import RxCocoa
import RxSwift
//@propertyWrapperの定義
@propertyWrapper
struct MyState<Value> {
let projectedValue: BehaviorRelay<Value>
var wrappedValue: Value {
get { return projectedValue.value }
@sakiyamaK
sakiyamaK / SamplePropertyWrapper.swift
Created June 22, 2022 12:17
PropertyWrapperの練習
import Foundation
import Combine
//@propertyWrapperの定義
@propertyWrapper
struct MyState<Value> {
private var value: Value
private(set) var projectedValue: PassthroughSubject<Value, Never> = .init()
@sakiyamaK
sakiyamaK / some.swift
Created June 14, 2022 15:31
swiftのsomeとはなんぞやのまとめ
//------- [準備] --------
protocol FoodProtocol {
var name: String { get }
var energy: Int { get }
}
struct Apple: FoodProtocol {
var name: String { "Apple" }
var energy: Int = 10
@sakiyamaK
sakiyamaK / mvvm_combine2.swift
Created April 3, 2022 14:55
UIKitなMVVMでCombine(inputsはfunc)
import UIKit
import Combine
import PlaygroundSupport
protocol ViewModelInput {
func updateFunc()
}
protocol ViewModelOutput {
var outValue1: AnyPublisher<Int, Never> { get }
@sakiyamaK
sakiyamaK / mvvm_combine1.swift
Last active April 3, 2022 14:56
UIKitなMVVMでCombine
import UIKit
import Combine
import PlaygroundSupport
protocol ViewModel {
//input
//update1はVC側でsink(受信)できてしまう
var update1: PassthroughSubject<Void, Never> { get }
//update2はVC側でパラメータが代入できてしまう
var update2: Published<Void>.Publisher { set get }
@sakiyamaK
sakiyamaK / MosaicLayout.swift
Created October 23, 2021 21:11
WWDC18のMosaicLayoutのリファクタリング
import UIKit
enum MosaicSegmentStyle: Int, CaseIterable {
case fullWidth = 0
case fiftyFifty
case twoThirdsOneThird
case oneThirdTwoThirds
func getNextMosaicSegmentStyle(retainItems: Int) -> MosaicSegmentStyle {
switch retainItems {