Skip to content

Instantly share code, notes, and snippets.

Avatar

Brennan Stehling brennanMKE

  • San Francisco, CA
View GitHub Profile
@brennanMKE
brennanMKE / DispatchTimer.swift
Last active Jan 20, 2022
Timer with Dispatch in Swift
View DispatchTimer.swift
import Foundation
public protocol AnyTimer: AnyObject {
static func scheduledTimer(withTimeInterval interval: TimeInterval, repeats: Bool, queue: DispatchQueue, block: @escaping (AnyTimer) -> Void) -> AnyTimer
init(withTimeInterval interval: TimeInterval, repeats: Bool, queue: DispatchQueue, block: @escaping (AnyTimer) -> Void)
func invalidate()
var isValid: Bool { get }
}
public class DispatchTimer: AnyTimer {
@brennanMKE
brennanMKE / MyTests.swift
Last active Jan 20, 2022
Swift Package Test Resources
View MyTests.swift
import XCTest
struct Sample: Codable {
let name: String
}
final class ResourceTests: XCTestCase {
let rootFilename = "Package.swift"
let resourcesDir = "Resources"
@brennanMKE
brennanMKE / IDETemplateMacros.plist
Last active Jan 5, 2022
Xcode File Header Templates
View IDETemplateMacros.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>FILEHEADER</key>
<string>
// Copyright © ___YEAR___ ___ORGANIZATIONNAME___.
// All Rights Reserved.
</string>
<key>ORGANIZATIONNAME</key>
@brennanMKE
brennanMKE / Playground.swift
Last active Dec 16, 2021
Character Surround Code Exercise
View Playground.swift
// Tweet: https://twitter.com/christianselig/status/1471197660111593473?s=12
let input = "|||||"
// option 1 - works but is not generic to be reused with other characters
input.replacingOccurrences(of: "|", with: "n|") + "n"
// option 2 - this function is more flexible and allows input to have any characters
func surround(input: String, spacer: String) -> String {
input.isEmpty ? input : input.map { "\(spacer)\($0)" }.joined() + spacer
@brennanMKE
brennanMKE / README.md
Last active Jan 5, 2022
Create SSH Key on Mac for Xcode
View README.md

Create SSH Key on Mac for Xcode

The docs for GitHub show a command to create a key with the ed25519 encryption method which is not allowed by Xcode. Even if you are not using the Source Control features in Xcode you will often need to use an account with GitHub when you are consuming Swift packages which are pulled from GitHub.

For SSH keys there are 4 algorithms.

  • 🚨 DSA: This is an older algorithm which is no longer supported and is superceded with more modern algorithms.
  • ⚠️ RSA: This algorithm was an improvement but it is now outdated and a more modern method should be used.
  • 👀 ECDSA: Another improvement which is dependent on your computer's ability to generate random numbers.
  • : Ed25519: The most recommended public-key algorithm today which you should use with GitHub.
@brennanMKE
brennanMKE / README.md
Created Dec 14, 2021
Die Shell Function
View README.md

Die Shell Function

This function simply writes to STDERR and exits with 1 which indicates to the caller there was an error.

@brennanMKE
brennanMKE / README.md
Created Dec 9, 2021
Locate Build Path
View README.md

Locate Build Path

When xcodebuild is run it will put the build directory under the Derived Data path. This script will locate that path. It requires a file named README.md to be in the project directory.

View findup.sh
#!/bin/sh
#
# Locates directory that contains $1.
findup()
{
DIR=$1
START="${DIR}"
while [ ! "${DIR}" -ef .. ]; do
[ -e "$1" ] && echo "$DIR" && return
@brennanMKE
brennanMKE / README.md
Last active Dec 8, 2021
Locate Checkout Paths
View README.md

Locate Checkout Paths

When dependencies are used with Swift Package Manager the remote Git repos will be cloned locally and placed into the build directory under Derived Data. This script will look for build directories which match the project directory and return the directory where dependency checkouts are located.

It works by reading the info.plist which can be found in each build directory under Derived Data. It has a value for WorkspacePath which is the source directory which ran the build. It could be the root directory for a Swift package but workspaces are also Xcode workspaces or even Xcode projects. The findup function is used to look for README.md to find the root of a project so that the workspace path is able to match what is in info.plist.


@brennanMKE
brennanMKE / README.md
Created Dec 8, 2021
Recreate iOS Simulators
View README.md

Recreate iOS Simulators

Apple includes a lot of profiles for iOS Simulators but you may not want them all since it creates a very long list that you just do not need. This script from Tom Insam (@tominsam) deletes all and creates just a small set of iOS Simulators.