Skip to content

Instantly share code, notes, and snippets.

View mbernson's full-sized avatar
👨‍💻
Coding...

Mathijs Bernson mbernson

👨‍💻
Coding...
View GitHub Profile
@mbernson
mbernson / gist:e830f34d1bcd88b8e7a97d21ca9dce24
Last active April 8, 2024 12:02
Compile libssh2 from source for macOS, creating a universal xcframework that works for Intel (x86_64) and Apple Silicon (arm64)
#!/usr/bin/env zsh
set -e
# This script compiles libssh2 from source for macOS, creating a universal xcframework that works for Intel (x86_64) and Apple Silicon (arm64).
SDK=macosx
SDK_VERSION="14.0"
# Set up a build directory
@mbernson
mbernson / MACAddress.swift
Last active February 5, 2024 16:20
MACAddress value type in Swift, for storing a MAC address
//
// MACAddress.swift
//
//
// Created by Mathijs on 23/01/2024.
//
import Foundation
/// Model for a standard MAC (Media Access Control) address, which consists of 6 bytes
@mbernson
mbernson / build_apple.sh
Last active April 8, 2024 12:02
Compile OpenSSL from source for macOS, creating a universal xcframework that works for Intel (x86_64) and Apple Silicon (arm64)
#!/usr/bin/env zsh
set -e
# This script compiles OpenSSL from source for macOS, creating a universal xcframework that works for Intel (x86_64) and Apple Silicon (arm64).
CROSS_COMPILE=`xcode-select --print-path`/Toolchains/XcodeDefault.xctoolchain/usr/bin/
CROSS_TOP=`xcode-select --print-path`/Platforms/MacOSX.platform/Developer
CROSS_SDK=MacOSX.sdk
ARCH="x86_64" # The x86_64 architecture preset also includes the ARM64 architecture.
@mbernson
mbernson / Alert+Error.swift
Last active January 25, 2024 13:55
Handling of (non-fatal) errors in SwiftUI
import SwiftUI
extension View {
/// Presents an alert when an error is present.
func alert<E: Error>(_ titleKey: LocalizedStringKey, error: Binding<E?>, buttonTitleKey: LocalizedStringKey = "Oke") -> some View {
modifier(ErrorAlert(error: error, titleKey: titleKey, buttonTitleKey: buttonTitleKey))
}
}
private struct ErrorAlert<E: Error>: ViewModifier {
@mbernson
mbernson / ContentView.swift
Last active July 14, 2023 17:04
SwiftUI custom segmented control
import SwiftUI
struct ContentView: View {
@State var selected = "Foo"
var body: some View {
SegmentedControl(options: ["Foo", "Bar", "Baz"], selectedOption: $selected) { label in
Text(label)
}
}
@mbernson
mbernson / ContentView.swift
Last active January 31, 2023 08:17
Button that starts/stops a screen recording and present the user with the Apple-provided preview
// How to use it:
import SwiftUI
struct ContentView: View {
var body: some View {
RecordScreenButton()
}
}
@mbernson
mbernson / arrays.c
Created December 14, 2022 12:19
C array on the heap
#include <stdio.h>
#include <stdlib.h>
typedef struct Point {
int x;
int y;
} Point;
int main() {
int count = 2;
@mbernson
mbernson / MarkdownExample.swift
Created August 2, 2022 13:35
Example of custom styles using Markdown support in SwiftUI Text.
import SwiftUI
import PlaygroundSupport
import Foundation
struct MarkdownView: View {
var body: some View {
Text(try! AttributedString(markdown: "This is a **basic** _string_. Italic bits are red. All the **bold** bits are _coloured_ **green**! And [this is a link!](https://q42.nl/).", customBoldColor: .green, customItalicColor: .red))
.font(.title)
}
}
@mbernson
mbernson / ScrollViewWrapper.swift
Last active April 26, 2022 13:07
UIScrollView wrapped for SwiftUI, with support for pull to refresh using the refreshable modifier
//
// ScrollViewWrapper.swift
//
// Created by Mathijs Bernson on 10/03/2022.
// Copyright © 2022 Q42. All rights reserved.
//
import SwiftUI
import UIKit
@mbernson
mbernson / MarkdownView.swift
Created April 1, 2022 11:32
Customizing the styling attributes of the bold text in a Markdown attributed string in Swift.
import SwiftUI
import Foundation
struct MarkdownView: View {
var body: some View {
Text(try! AttributedString(markdown: "This is a **basic** _string_.", customBoldColor: .green))
.font(.title)
}
}