Skip to content

Instantly share code, notes, and snippets.

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

Yusuke Hosonuma YusukeHosonuma

🏠
Working from home
View GitHub Profile
@bannzai
bannzai / add_sendable.sh
Last active September 3, 2024 14:12
struct, enumに無心でSendableをつけるsed。Viewは除外
#!/bin/bash
TARGET_DIR=$1
echo "やるお"
for file in $(find "$TARGET_DIR" -name "*.swift"); do
# struct User: * { の場合。ただし、View適応は除く
sed -i '' -E '/struct [A-Za-z0-9_]+: [^{]*View[^{]*\{/!s/(struct [A-Za-z0-9_]+: [^{]*)\{/\1, Sendable {/g' "$file"
sed -i '' -E '/enum [A-Za-z0-9_]+: [^{]*View[^{]*\{/!s/(enum [A-Za-z0-9_]+: [^{]*)\{/\1, Sendable {/g' "$file"
@Koshimizu-Takehito
Koshimizu-Takehito / StateObject_init.swift
Last active September 22, 2022 09:39
StateObject(wrappedValue:) を直接呼び出すと余計なオブジェクトを作る
import SwiftUI
// MARK: - ObservableObject
final class Counter: ObservableObject {
@Published private(set) var count: Int
deinit {
print("🍂", #function, count)
}
@chriseidhof
chriseidhof / ContentView.swift
Last active March 27, 2024 19:14
Variadic Views
import SwiftUI
struct MyValue: _ViewTraitKey {
static var defaultValue: Int = 0
}
extension View {
func myValue(_ value: Int) -> some View {
_trait(MyValue.self, value)
}
import SwiftUI
import class UIKit.UIImage
struct AppleLogo: View {
private let colors: [Color] = [
.green,
.green,
.green,
.yellow,
.orange,
extension Task where Failure == Error {
// Start a new Task with a timeout. If the timeout expires before the operation is
// completed then the task is cancelled and an error is thrown.
init(priority: TaskPriority? = nil, timeout: TimeInterval, operation: @escaping @Sendable () async throws -> Success) {
self = Task(priority: priority) {
try await withThrowingTaskGroup(of: Success.self) { group -> Success in
group.addTask(operation: operation)
group.addTask {
try await _Concurrency.Task.sleep(nanoseconds: UInt64(timeout * 1_000_000_000))
import SwiftUI
extension AttributedString {
// call this init to make all the `code` block highlighted in specific way:
init(codeHightlighted key: String.LocalizationValue) {
var result = AttributedString(localized: key)
let highlight = AttributeContainer.foregroundColor(.red).backgroundColor(.mint)
result.replaceAttributes(AttributeContainer.inlinePresentationIntent(.code), with: highlight)
self = result
}
/// - seealso: https://www.hackingwithswift.com/articles/212/whats-new-in-swift-5-2
import Foundation
protocol OptionalType {
associatedtype Wrapped
var value: Wrapped? { get }
}
extension Optional: OptionalType {
@AliSoftware
AliSoftware / Demo.swift
Last active October 31, 2023 12:25
NestableCodingKey: Nice way to define nested coding keys for properties
struct Contact: Decodable, CustomStringConvertible {
var id: String
@NestedKey
var firstname: String
@NestedKey
var lastname: String
@NestedKey
var address: String
enum CodingKeys: String, NestableCodingKey {
// Advanced SwiftUI Transitions
// https://swiftui-lab.com
// https://swiftui-lab.com/advanced-transitions
import SwiftUI
struct CrossEffectDemo: View {
let animationDuration: Double = 2
let images = ["photo1", "photo2", "photo3", "photo4"]
@State private var idx = 0