Skip to content

Instantly share code, notes, and snippets.

View timsneath's full-sized avatar

Tim Sneath timsneath

View GitHub Profile
@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 {
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
@timsneath
timsneath / analyzer.log
Created October 29, 2021 03:07
Analyzer Failure
This file has been truncated, but you can view the full file.
1635476713209:Ver:1596173960672524124772:VS-Code:3.27.2:1.32.7:2.15.0
void main() {
const address = -69795452;
const UINT32_MAX = 0xFFFFFFFF;
// Two's complement
print((UINT32_MAX - address.abs() + 1).toRadixString(16));
}