Skip to content

Instantly share code, notes, and snippets.

View 0x41c's full-sized avatar
🍔
borgor

Corban Amouzou 0x41c

🍔
borgor
View GitHub Profile
@0x41c
0x41c / segment-dumper.swift
Last active March 17, 2022 20:34
Dump macho segment names from swift
//
// segment-dumper.swift
//
// Created by Cero on 2021-10-03.
//
import Foundation
import ArgumentParser
import MachO.loader
import MachO.swap
@larsaugustin
larsaugustin / ExampleView.swift
Created August 19, 2020 16:59
Conditional view modifiers in SwiftUI
struct ExampleView: View {
var body: some View {
Text("Example")
.if(true) { $0.foregroundColor(.red) }
}
}
@Azoy
Azoy / syscall.swift
Last active August 25, 2023 21:49
Raw system calls in Swift
// macOS x86_64 syscall works as follows:
// Syscall id is moved into rax
// 1st argument is moved into rdi
// 2nd argument is moved into rsi
// 3rd argument is moved into rdx
// ... plus some more
// Return value is stored in rax (where we put syscall value)
// Mac syscall enum that contains the value to correctly call it
enum Syscall: Int {
@NSExceptional
NSExceptional / Private class usage in Swift.md
Last active February 21, 2022 22:40
An example of how to use a private class from Swift. It's not pretty, but it isn't too bad.

Using private Objective-C classes from Swift

In Objective-C, if you want to make use of one of Apple's internal classes, all you have to do is declare the class's interface with the methods you want to use. From there you can create instances of the class by using the NSClassFromString function to obtain a reference to the class object, like so:

[[NSClassFromString(@"_NSSomeClass") alloc] initWithFoo:5]

In Swift, this is not so easy. Referencing the type of the "hollow" Objective-C class interface anywhere in Swift code makes the compiler query the class for type information that is not known at compile time. This presents a huge problem. You cannot dynamically initialize a type either. Though, it seems the following code used to work at one point: