Skip to content

Instantly share code, notes, and snippets.

View brennanMKE's full-sized avatar

Brennan Stehling brennanMKE

View GitHub Profile
@brennanMKE
brennanMKE / XcodeWarnings.sh
Created September 11, 2023 20:51
Xcode Warnings Script
TAGS="TODO:|FIXME:"
ERRORTAG="ERROR:"
find "${SRCROOT}" \( -name "*.h" -or -name "*.m" -or -name "*.swift" \) -print0 | xargs -0 egrep --with-filename --line-number --only-matching "($TAGS).*\$|($ERRORTAG).*\$" | perl -p -e "s/($TAGS)/ warning: \$1/" | perl -p -e "s/($ERRORTAG)/ error: \$1/"
@brennanMKE
brennanMKE / README.md
Last active September 5, 2023 17:32
Bookmarklets for Swift language developers

Bookmarklets for Swift language developers

What is a bookmarklet? Basically it is a bit of JavaScript which you can store as a bookmark. The ones below prompt the user for a search term and immediately opens a URL to Google with the site restricted to get targetted search results. Simply bookmark any page then edit your bookmarks. Replace the new bookmark you just created with a useful title and the code below for each bookmark. Place them in your Favorites to show them at the top of Safari. You can also put them into a group so you can see them in a drop down list.

Apple Docs

javascript:(function() { var s = prompt("Apple Docs:"); var d = "https://www.google.com/search?q=" + encodeURIComponent(s + " site:developer.apple.com"); window.location = d;})()
@brennanMKE
brennanMKE / Debug.swift
Created September 3, 2023 20:51
Demangle and log call static symbols during runtime
// Revised code written by Naruki Chigira under MIT license.
// https://github.com/naru-jpn/CallStackSymbols
// See: https://stackoverflow.com/questions/24321773/how-can-i-demangle-a-swift-class-name-dynamically
// Darwin/Swift: https://github.com/apple/swift/blob/main/stdlib/public/runtime/Demangle.cpp#L913
#if DEBUG
import Foundation
import Darwin
@brennanMKE
brennanMKE / README.md
Created May 25, 2023 17:52
Git Support Commands

Git Support Commands

pull-main will look in every immediate subfolder for a .git directory and then compare the branch to the given branch. The branch will default to main if none was given.

Usage: pull-main feature-branch

list-branches will also look in every immediate subfolder for a .git directory and then print the name of the repo and the branch name.

Usage: list-branches

private func logCallStack(tag: String = "[TAG]") {
let lines = Thread.callStackSymbols[1...]
for line in lines {
logger.debug("\(tag) \(line)")
}
}
@brennanMKE
brennanMKE / Playground.swift
Created April 25, 2023 17:46
Double formatting
import Foundation
extension Double {
var twoDecimalPlaces: String {
formatted(.number.precision(.fractionLength(2)))
}
}
let value = 1.0 / 3.0
value
@brennanMKE
brennanMKE / IceCreamTruck.swift
Created February 24, 2023 19:17
Ice Scream Truck Async Notifications
import Foundation
extension Notification.Name {
static let IceCreamTruckNotification = Notification.Name(rawValue: "IceCreamTruckNotification")
}
actor IceCreamTruck {
func monitor() async {
let sequence = NotificationCenter.default.notifications(named: .IceCreamTruckNotification)
for await name in sequence.map({ $0.name }) {
@brennanMKE
brennanMKE / Build.xcconfig
Created February 17, 2023 19:13
Swift to Objective-C Bridging Header Build Setting
SWIFT_OBJC_BRIDGING_HEADER = $(TARGET_NAME)/$(TARGET_NAME)-Bridging-Header.h
SWIFT_INSTALL_OBJC_HEADER = NO
DEFINES_MODULE = YES

User Default Property Wrapper

Using UserDefaults in a get/set property requires some boilerplate unless you make use of a property wrapper which handles it for you. Then all you need to provide is the key and default value. That is what this code does.

@brennanMKE
brennanMKE / Playground.swift
Last active November 4, 2022 17:41
Working Actor using Global Actor in Swift
import Foundation
@globalActor public final actor WorkingActor {
public static let shared = WorkingActor()
}
// Copied from MainActor
extension WorkingActor {
/// Execute the given body closure on the working actor.
public static func run<T>(resultType: T.Type = T.self, body: @WorkingActor @Sendable () throws -> T) async rethrows -> T where T : Sendable {