Skip to content

Instantly share code, notes, and snippets.

View tib's full-sized avatar
🪶
Just flying around...

Tibor Bödecs tib

🪶
Just flying around...
View GitHub Profile
import SwiftUI
import PlaygroundSupport
struct Sunset: View {
@State var backgroundColor = Color.blue
@State var sunSetted = false
let sunGradient = [Color.yellow, Color.orange]
let moonGradient = [Color.gray, Color.black]
@State var alignment = Alignment.top
@mergesort
mergesort / AnimationDemo.swift
Last active December 18, 2023 02:34
A SwiftUI prototype animation for adding/removing players from a game
import SwiftUI
let colors = [
Color.pink,
Color.blue,
Color.green,
Color.orange,
Color.purple,
Color.black,
]
// a.swift
public dynamic func f() {
print("original")
}
// b.swift
import a
@_dynamicReplacement(for: f())
public func f_b() {
#! /usr/bin/swift
//
// - This is just some AppKit boilerplate to launch a window.
//
import AppKit
@available(OSX 10.15, *)
class AppDelegate: NSObject, NSApplicationDelegate {
let window = NSWindow()
let windowDelegate = WindowDelegate()
@artyom-stv
artyom-stv / gist:a87f572999074aa9afab03d8e96eef2e
Last active August 2, 2024 18:21
Visitor-based approach for accessing `@ViewBuilder`-provided content
// SwiftUI public API
public protocol View {
associatedtype Body: View
var body: Self.Body { get }
}
extension Never: View {
public typealias Body = Never
@AvdLee
AvdLee / largest-file.sh
Last active December 10, 2023 19:24
Find your project's Swift file with the most lines of code
find . -type f -name "*.swift" -exec grep -H -c '[^[:space:]]' {} \; | \sort -nr -t":" -k2 | awk -F: '{printf("Your largest file %s contains: %s lines \n", $1, $2); exit;}'
@AliSoftware
AliSoftware / Bindings.swift
Last active August 5, 2025 14:50
Re-implementation of @binding and @State (from SwiftUI) myself to better understand it
/*:
This is a concept re-implementation of the @Binding and @State property wrappers from SwiftUI
The only purpose of this code is to implement those wrappers myself
just to understand how they work internally and why they are needed,
⚠️ This is not supposed to be a reference implementation nor cover all
subtleties of the real Binding and State types.
The only purpose of this playground is to show how re-implementing
them myself has helped me understand the whole thing better
@mxcl
mxcl / detweet.swift
Last active August 16, 2024 15:38
Delete all tweets and favorites older than two months ago. Instructions in comment.
#!/usr/bin/swift sh
import Foundation
import PromiseKit // @mxcl ~> 6.5
import Swifter // @mattdonnelly == b27a89
let swifter = Swifter(
consumerKey: "FILL",
consumerSecret: "ME",
oauthToken: "IN",
oauthTokenSecret: "https://developer.twitter.com/en/docs/basics/apps/overview.html"
@bugaevc
bugaevc / fork.swift
Created October 8, 2018 20:24
Calling fork() from Swift
// Note: calling fork() can be unsafe and dangerous, that
// is why Swift doesn't let you invoke it directy. Be very
// careful about how you use fork(). For some more details,
// see https://www.evanjones.ca/fork-is-dangerous.html and
// http://www.sealiesoftware.com/blog/archive/2017/6/5/Objective-C_and_fork_in_macOS_1013.html
import Darwin
let RTLD_DEFAULT = UnsafeMutableRawPointer(bitPattern: -2)
let forkPtr = dlsym(RTLD_DEFAULT, "fork")
@GeorgeLyon
GeorgeLyon / Rust vs. Swift.md
Last active November 20, 2025 11:20
A list of advantages Swift has over Rust

Note This is a little out of date. Rust has made some progress on some points, however many points still apply.

Philosophy

Progressive disclosure

Swift shares Rust's enthusiasm for zero-cost abstractions, but also emphasizes progressive disclosure. Progressive disclosure requires that language features should be added in a way that doesn't complicate the rest of the language. This means that Swift aims to be simple for simple tasks, and only as complex as needed for complex tasks.

The compiler works for you