Skip to content

Instantly share code, notes, and snippets.

View toshi0383's full-sized avatar
🏠
Working from home

Toshihiro Suzuki toshi0383

🏠
Working from home
  • Tokyo
View GitHub Profile
import Foundation
// First one fails to be decoded.
//
// [1] dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Unescaped control character '0xa' around line 2, column 0." UserInfo={NSJSONSerializationErrorIndex=16, NSDebugDescription=Unescaped control character '0xa' around line 2, column 0.})))
// [2] Success!
//
// SeeAlso: https://github.com/apple/swift-evolution/blob/main/proposals/0200-raw-string-escaping.md
let literalJSONishString = """
#!/bin/bash
# プロジェクトファイルのパス
PROJECT_PATH="YourProject.xcodeproj"
# スキーム名
SCHEME_NAME="YourScheme"
# シミュレータのデバイス名
SIMULATOR_DEVICE_NAME="iPhone 12"
@toshi0383
toshi0383 / CodePiece.swift
Created February 1, 2023 10:50
ValveProperty #CodePiece
/// 弁(Valve)のように値がこなくなったら自動的に元の値に戻るProperty。
/// 元に戻る時間も指定できる。
@propertyWrapper
struct ValveProperty<Value> {
private let relay: BehaviorRelay<Value>
private let property: Property<Value>
init(
automaticallyTogglesToInitialValue initialValue: Value,
afterInterval interval: RxTimeInterval = .milliseconds(1000),
// comparing atomic operation performance
import Foundation
final class Locking {
static let shared = Locking()
private(set) var isLocked: Bool = false
private let _lock = NSLock()
// comparing atomic operation performance
import Concurrency
import Foundation
final class Locking {
static let shared = Locking()
private(set) var isLocked: Bool = false
@toshi0383
toshi0383 / IsCase.stencil
Last active November 1, 2019 04:05
Generate useful extension when filtering enum values
{% for enum in types.enums %}
// MARK: {{ enum.name }}
extension {{ enum.name }} {
{% for case in enum.cases %}
{{ enum.accessLevel }} var is{{ case.name|upperFirstLetter }}: Bool {
switch self { case .{{ case.name }}: return true; default: return false; }
}
{% endfor %}
}
{% endfor %}
import Foundation
let smp = DispatchSemaphore(value: 1)
let smp1 = DispatchSemaphore(value: 0)
func doSomething() {
for i in (0..<4) {
smp.wait()
print("[\(i * 3)]")
defer {
let hotdogProperty = BehaviorSubject<String>(value: "")
Observable<Int>
.interval(1.0, scheduler: ConcurrentMainScheduler.instance)
.map { "Nice \($0)" }
.startWith("bonapetit!")
.debug("[source]")
.subscribe(onNext: { hotdogProperty.onNext($0) })
func hotdogObservable() -> Observable<String> {
return hotdogProperty.asObservable()
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {