Skip to content

Instantly share code, notes, and snippets.

View timsneath's full-sized avatar

Tim Sneath timsneath

View GitHub Profile

Apple developer documentation

developer.apple.com pages are rendered with JavaScript, so fetch them as Markdown instead. For /documentation pages, add .md to the URL (https://developer.apple.com/documentation/swiftui/view.md). For /design and /tutorials pages, also insert /tutorials/data before the path (https://developer.apple.com/tutorials/data/design/human-interface-guidelines/buttons.md). A few tutorial pages, such as overviews, have no Markdown version; for those, use .json in place of .md (https://developer.apple.com/tutorials/data/tutorials/swiftui.json).

import Foundation
struct AnimatedBirthdayCake {
let name: String
// Tier character widths
private let topWidth = 21
private let midWidth = 25
private let baseWidth = 29
# 1) Prereqs
sudo apt update
sudo apt install -y cmake pkg-config build-essential zlib1g-dev liblzma-dev git
# 2) Fetch the last series that still provides SONAME .2
mkdir -p ~/opt/src && cd ~/opt/src
git clone https://gitlab.gnome.org/GNOME/libxml2.git
cd libxml2
git checkout v2.12.7
@timsneath
timsneath / hello-win32.swift
Created March 3, 2024 00:46
Simple example of calling classic Windows APIs from Swift
import WinSDK
// Call a simple Win32 function and pass in strings
MessageBoxA(nil, "Hello from Swift running on Windows", "Swift Message Box", UINT(MB_OK))
// Call a Win32 function that passes a variable by pointer
var memorySizeInBytes: LONGLONG = 0
GetPhysicallyInstalledSystemMemory(&memorySizeInBytes)
print("System has \(memorySizeInBytes / 1024 / 1024)GB of RAM installed.");
@timsneath
timsneath / dragcard.swift
Created October 17, 2023 04:46
Small sample of a draggable card object in SwiftUI
// Adapted from Hacking with SwiftUI, credit to Paul Hudson (@twostraws).
// https://www.hackingwithswift.com/books/ios-swiftui/animating-gestures
import SwiftUI
struct ContentView: View {
@State private var dragAmount = CGSize.zero
var body: some View {
RoundedRectangle(cornerRadius: 10)
@timsneath
timsneath / reverseSort.swift
Created September 29, 2023 19:28
Sorting in reverse, using an idiomatic Swift approach.
/// Let's sort this array in reverse alphabetical order. Credit to @JoaquinAlori at @tryolabs for the idea.
let names = ["Mavericks", "Yosemite", "El Capitan", "Sierra", "High Sierra",
"Mojave", "Catalina", "Big Sur", "Monterey", "Ventura", "Sonoma"]
var reversedNames : [String]
/// ✅ Sort method returns true when the first element should be ordered before the second.
func backward(_ a: String, _ b: String) -> Bool {
return a > b
}
reversedNames = names.sorted(by: backward)
@timsneath
timsneath / ContentView.swift
Created September 22, 2023 23:59
Replace the default `ContentView` struct with this...
struct ContentView: View {
@State private var switchSetting = true
@State private var stepperValue = 11
@State private var dateValue = Date.now
var body: some View {
NavigationView {
Form {
Section {
Toggle("On or off: you choose", isOn: $switchSetting)
@timsneath
timsneath / main.dart
Last active March 11, 2023 23:08
num-ranges
class Range<T extends num> {
final T from;
final T to;
num get magnitude => to - from;
Range(this.from, this.to);
factory Range.fromList(List<T> list) {
assert(list.length == 2);
// A simple example of records and patterns
(double x, double y) getLocation(String name) {
if (name == 'Nairobi') {
return (1.2921, 36.8219);
}
else {
return (0, 0);
}
}
abstract class Animal {
void makeNoise();
}
class Dog extends Animal {
@override
void makeNoise() => print('Woof');
}
class Cat extends Animal {