Skip to content

Instantly share code, notes, and snippets.

View mluisbrown's full-sized avatar

Michael Brown mluisbrown

View GitHub Profile
@mluisbrown
mluisbrown / xstrings.swift
Last active June 29, 2022 15:09
Swift script to extract all string literals from a Swift source file in the format of a Localizable.strings file
#!/usr/bin/swift
import Foundation
func processFile(_ path: String, keyPrefix: String?) {
guard let file = freopen(path, "r", stdin) else {
fputs("Failed to open file.\n", stderr)
return
}
defer {
fclose(file)
@mluisbrown
mluisbrown / Rx2Ras-Cheatsheet.md
Last active May 19, 2024 10:47
An RxSwift to ReactiveSwift cheatsheet
@mluisbrown
mluisbrown / SwiftUIButtonBugView.swift
Created September 22, 2019 14:03
Example view that demonstrates bug with Buttons in Lists in SwiftUI
import SwiftUI
struct ContentView : View {
var body: some View {
return NavigationView {
VStack {
List {
Section(header: Text("Credentials").font(.body).padding([.top, .bottom])) {
HStack {
Text("Email:")
@mluisbrown
mluisbrown / FixBTSound.applescript
Last active September 29, 2024 00:29
AppleScript to set macOS audio input device to "Internal Microphone"
-- Sets your audio input source to "Internal Microphone"
-- Frequently needed if you use bluetooth headpohones and
-- run the Xcode iOS simulator, which will often set your
-- headphones to be the input device, resulting in a drastic
-- decrease in sound quality, and making it mono
tell application "System Preferences" to activate
tell application "System Preferences"
reveal anchor "input" of pane id "com.apple.preference.sound"
end tell
@mluisbrown
mluisbrown / index.ts
Last active October 11, 2017 11:32
Simple JSON validation example using RamdaJS and fp-ts with Typescript
import { both, converge, equals as eq, ifElse, not, pipe, toLower, type, keys, curry } from "ramda"
import { Either, left, right, isLeft, isRight, getOrElse } from "fp-ts/lib/Either"
export type Json = string|number|boolean|Date|JsonObject
interface JsonObject {
[x: string]: Json;
}
interface JsonArray extends Array<Json> { }
@mluisbrown
mluisbrown / Array2Dict.swift
Last active December 14, 2016 20:54
Generic Swift function to convert an Array into a Dictionary using a key from each object in the Array
func dict<T, U: Hashable>(from array: [T], getKey: (T) -> U) -> [U : T] {
var dict: [U : T] = .init(minimumCapacity: array.count)
array.forEach {
dict[getKey($0)] = $0
}
return dict
}
@mluisbrown
mluisbrown / main.swift
Last active July 19, 2016 15:08
UTF16 encode and decode
import Foundation
func encodeString(str: String) -> String {
return Array(str.utf16).map {
String(format: "%04x", $0)
}.reduce("") {
return $0 + $1
}
}
@mluisbrown
mluisbrown / main.swift
Created July 10, 2016 11:58
Swift code to find and count all Circular Primes below 1 million (Project Euler problem 35)
// Circular primes
// Problem 35
// The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.
//
// There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.
//
// How many circular primes are there below one million?
import Foundation
@mluisbrown
mluisbrown / AppDelegate.h
Last active July 22, 2022 07:20
PersistentStack for using Core Data with iCloud sync in iOS 7, adapted from code by Chris Eidhof for objc.io #4 (http://www.objc.io/issue-4/full-core-data-application.html). The most important thing is that you don't have to know whether the user is using iCloud or not, or even has an iCloud account on their device. Core Data now transparently h…
@import UIKit;
#import "PersistentStack.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end