Skip to content

Instantly share code, notes, and snippets.

View mredig's full-sized avatar
🤓
1s and 0s aren't going to put themselves in the right order.

Michael mredig

🤓
1s and 0s aren't going to put themselves in the right order.
View GitHub Profile
@mredig
mredig / SMJobBlessUtil-python3.py
Created April 14, 2023 20:15 — forked from mikeyh/SMJobBlessUtil-python3.py
SMJobBlessUtil.py for Python3 / Xcode 13.3 / macOS Monterey 12.3 (with workaround for otool arm64 weirdness)
#! /usr/bin/python3
#
# File: SMJobBlessUtil.py
#
# Contains: Tool for checking and correcting apps that use SMJobBless.
#
# Written by: DTS
#
# Copyright: Copyright (c) 2012 Apple Inc. All Rights Reserved.
#
@mredig
mredig / AsyncTaskQueue.swift
Last active April 5, 2022 20:14
Since `Task`s are not guaranteed to execute in the order they are created, yet it is sometimes important for them to be completed in order, this actor allows for you to add async closures to a queue in which the order is FIFO.
import Foundation
/**
Since `Task`s are not guaranteed to execute in the order they are created, yet it is sometimes important for them to be completed in order, this actor allows for you to
add async closures to a queue in which the order is FIFO.
*/
public actor AsyncTaskQueue {
public typealias TaskType = Task<Void, Error>
public private(set) var currentTask: TaskType? {
didSet {
// This causes a retain cycle on `vc`
var vc: UIViewController?
let myNewSUIView = ASwiftUIView(
completion: { url in
vc?.dismiss(animated: true)
})
let hostingController = UIHostingController(rootView: myNewSUIView)
@mredig
mredig / hidden strong reference cycle closure.swift
Created January 16, 2021 01:01
an example of an easy accident waiting to happen
//: Playground - noun: a place where people can play
import Foundation
class Bar {
let doingSomething: () -> Void
init(doingSomething: @escaping () -> Void) {
self.doingSomething = doingSomething
}
import Foundation
/// Makes an Encodable and Decodable properties encode to `null` instead of omitting the value altogether.
@propertyWrapper
public struct NullCodable<T> {
public var wrappedValue: T?
public init(wrappedValue: T?){
self.wrappedValue = wrappedValue
}
//
// main.m
// Space Age
//
// Created by Michael Redig on 10/9/19.
// Copyright © 2019 Red_Egg Productions. All rights reserved.
//
#import <Foundation/Foundation.h>
func makeChangeAsString(fromAmount amount: Double, withCost cost: Double) -> String {
let change = amount - cost
let dollars: Int
var changeRemaining: Double = 0
(dollars, changeRemaining) = getCoinCount(1.0, fromChange: change)
let quarters: Int
(quarters, changeRemaining) = getCoinCount(0.25, fromChange: changeRemaining)
let dimes: Int
(dimes, changeRemaining) = getCoinCount(0.1, fromChange: changeRemaining)
//: Playground - noun: a place where people can play
import Foundation
func numberOfVowels(in string: String) -> Int {
var numberOfVowels = 0
for vowel in string.lowercased() {
switch vowel {
case "a", "e", "i", "o", "u":
numberOfVowels = numberOfVowels + 1
@mredig
mredig / sameNumberOfBinaryOnes.swift
Last active June 19, 2019 15:48
takes any positive integer as an argument and returns the next highest and next lowest integers having the same number of 1s as the passed argument's binary representation
import Foundation
extension FixedWidthInteger {
func sameNumberOfBinaryOnes() -> (Self?, Self?) {
guard self != 0 else { return (nil,nil) }
let myCount = binaryOneCount()
var nextHigher: Self?
for maybe in (self + 1)...Self.max {
@mredig
mredig / Is Twin Prime.swift
Last active June 12, 2019 15:36
checks to see if a number is a twin prime
// a twin prime is where a prime number +/- 2 is another prime number. For example, 3 is prime, and so is 5. They are twin primes. (Since 7 is 2 away from 5 as well, maybe that makes them triplets! But I'm not a mathematician. I'll let them decide.)
extension FixedWidthInteger {
func isPrime() -> Bool {
// negatives, 0, and 1 are all not prime and will break the range created below
guard self >= 2 else { return false }
// no even numbers are prime, except 2
guard !self.isMultiple(of: 2) else {
return self == 2 ? true : false