Skip to content

Instantly share code, notes, and snippets.

@nanoxd
nanoxd / run.sh
Created September 27, 2018 10:52
[Debugging iOS] Useful ways to check your binary
# Display shared libraries that your binary attempts to load
otool -L <binary>
# Display DYLD symbols for exports and imports
nm <binary>
@nanoxd
nanoxd / playground.swift
Created October 1, 2018 13:25
[XCTest in Playgrounds] Use XCTest in Xcode Playground
import XCTest
class MyTestCase: XCTestCase {
func testExample() {
XCTAssertTrue(true)
}
}
MyTestCase.defaultTestSuite.run()
@nanoxd
nanoxd / MulticastDelegate.swift
Created October 5, 2018 00:20
[MulticastDelegate] #swift Utility Class that can hold on to any number of delegates and invoke arbitrary blocks of code on them
/// Utility Class that can hold on to any number of delegates and invoke arbitrary blocks of code on them
public class MulticastDelegate<T: AnyObject> {
/// HashTable containing any number of delegates, held weakly
private let delegates: NSHashTable<AnyObject> = NSHashTable.weakObjects()
/// Adds delegate to list of delegates
///
/// - Parameter delegate: Delegate to add to list of known delegates
public func add(_ delegate: T) {
delegates.add(delegate as AnyObject)
@nanoxd
nanoxd / DispatchQueue+isMain.swift
Created October 12, 2018 11:26
[DispatchQueue+isMain] Determine if the current GCD Queue is the main queue #swift
extension DispatchQueue {
fileprivate static let mainQueueKey = DispatchSpecificKey<()>()
static func configureMainQueue() {
main.setSpecific(key: mainQueueKey, value: ())
}
}
public extension DispatchQueue {
/// Easy and safe way of checking if the current queue is the main queue
@nanoxd
nanoxd / HasCustomView.swift
Created October 16, 2018 14:14
[HasCustomView] Define a custom property for `UIViewController` to use a typed view #swift #ios
/// The HasCustomView protocol defines a customView property for UIViewControllers to be used in exchange of the regular view property.
/// In order for this to work, you have to provide a custom view to your UIViewController at the loadView() method.
public protocol HasCustomView {
associatedtype CustomView: UIView
}
extension HasCustomView where Self: UIViewController {
/// The custom typed view
public var customView: CustomView {
guard let customView = view as? CustomView else {
@nanoxd
nanoxd / NSObject+AssociatedObject.swift
Created October 16, 2018 14:25
[NSObject+AssociatedObject] Wrapper around associated objects #swift
public enum AssociationPolicy {
case assign
case retain
case copy
case retainNonatomic
case copyNonatomic
fileprivate var policy: objc_AssociationPolicy {
switch self {
@nanoxd
nanoxd / UIWindow+TopLevel.swift
Created October 31, 2018 00:20
[UIWindow+TopLevel] Add a new window that rests above the key window #swift #ios
extension UIWindow {
final class StatusBarPreferringViewController: UIViewController {
// MARK: - Inputs
private let statusBarStyle: UIStatusBarStyle
// MARK: - Initialization
init(statusBarStyle: UIStatusBarStyle)
self.statusBarStyle = statusBarStyle
@nanoxd
nanoxd / FixBTSound.applescript
Last active November 11, 2018 14:26 — forked from mluisbrown/FixBTSound.applescript
[FixBTSound] 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
@nanoxd
nanoxd / lint
Created November 11, 2018 14:27
[bin/lint] Binary to run swiftlint, meant to be placed at the root of the project #sh
#!/bin/sh
#
# Lint any changed files using swiftlint.
#
# This works both independently and as a run phase script.
# Path to installed version of Swift lint.
readonly SWIFT_LINT_PATH=$(which swiftlint)
if [ -z "$SWIFT_LINT_PATH" ]; then
@nanoxd
nanoxd / fix-xcode
Last active March 15, 2019 12:17
Xcode Scripts
#!/usr/bin/env sh
# Xcode sometimes gets sad and won't index or other shenanigans. You can easily cheer up Xcode
# and get things happy again by simply removing some files. This little script does just that.
# Quit Xcode
osascript -e 'tell app "Xcode" to quit'
# Remove derived data
rm -rf ~/Library/Developer/Xcode/DerivedData/*