Skip to content

Instantly share code, notes, and snippets.

@willbrandin
willbrandin / PinEntryView.swift
Created October 23, 2020 13:57
A SwiftUI View for entering login pins. Supports first responder and has no third parties.
import SwiftUI
struct PinEntryView: View {
var pinLimit: Int = 4
var isError: Bool = false
var canEdit: Bool = true
@Binding var pinCode: String
private var pins: [String] {
import UIKit
class RootViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let text = "Cute cat"
let image = UIImage(named: "cat.jpg")!
// Note: you can't wrap the text and image into a single "TextImage" object and selectively return both or one as UIActivityItemSource doesn't like holding arrays
@steipete
steipete / Runtime.m
Created August 7, 2019 08:59
pspdf_swizzleSelectorWithBlock, pspdf_swizzleSelector. Please use your own prefix when you copy these.
// http://defagos.github.io/yet_another_article_about_method_swizzling/ (Thank you!!)
// Returns the original implementation
static _Nullable IMP pspdf_swizzleSelector(Class clazz, SEL selector, IMP newImplementation) {
NSCParameterAssert(clazz);
NSCParameterAssert(selector);
NSCParameterAssert(newImplementation);
// If the method does not exist for this class, do nothing.
const Method method = class_getInstanceMethod(clazz, selector);
if (!method) {
@cellularmitosis
cellularmitosis / EmojiPointersDemo.swift
Created August 15, 2018 18:11
Representing pointer values as emoji can be useful for "visually" debugging certain issues, like cell reuse, etc.
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let window = UIWindow(frame: UIScreen.main.bounds)
@ole
ole / UIAlertController+TextField.swift
Last active September 13, 2022 14:20
A UIAlertController with a text field and the ability to perform validation on the text the user has entered while the alert is on screen. The OK button is only enabled when the entered text passes validation. More info: https://oleb.net/2018/uialertcontroller-textfield/
import UIKit
/// A validation rule for text input.
public enum TextValidationRule {
/// Any input is valid, including an empty string.
case noRestriction
/// The input must not be empty.
case nonEmpty
/// The enitre input must match a regular expression. A matching substring is not enough.
case regularExpression(NSRegularExpression)
@coreyd303
coreyd303 / nudge.py
Last active March 2, 2024 03:49
Debugging with lldb and xCode
""" File: nudge.py
An lldb Python script to add a nudge command.
Add to ~/.lldbinit:
command script import ~/path/to/nudge.py
Usage:
(lldb) nudge x-offset y-offset [view]
@5818959
5818959 / gist:b9f392d75ea2ca0d09cb63bf645bc666
Created August 24, 2017 08:06
Git diff between two commits filtered by files extension
# full diff
git diff <SHA1> <SHA1> -- `find . -name '<pattern>'`
# only file names
git diff <SHA1> <SHA1> --name-status -- `find . -name '<pattern>'`
@steipete
steipete / PSPDFGenerics.h
Last active April 28, 2020 15:08
Override copy and mutableCopy on Objective-C collection classes to pass along both the collection type and the generic info. This is a header-only "library". MIT licensed. Craving for more? foreach: https://gist.github.com/steipete/7e3c69b985165dc23c5ec169b857ff42 even more: https://pspdfkit.com/blog/2016/swifty-objective-c/ - ships in https://p…
//
// PSPDFGenerics.h
// PSPDFFoundation
//
// PSPDFKit is the leading cross-platform solution for integrating PDFs into your apps: https://pspdfkit.com.
// Try it today using our free PDF Viewer app: https://pdfviewer.io/
//
// This file is MIT licensed.
/**
@gnou
gnou / TextSize.swift
Created March 21, 2017 10:09
Calculate height of some text when width is fixed
public struct TextSize {
fileprivate struct CacheEntry: Hashable {
let text: String
let font: UIFont
let width: CGFloat
let insets: UIEdgeInsets
fileprivate var hashValue: Int {
return text.hashValue ^ Int(width) ^ Int(insets.top) ^ Int(insets.left) ^ Int(insets.bottom) ^ Int(insets.right)
@steipete
steipete / Warnings.xcconfig
Last active March 13, 2018 14:47
Did you knew that Clang Analyzer as alpha checkers? Early Christmas is here! (Ignore the rest... we run our PDF SDK https://pspdfkit.com with -Weverything because warnings are awesome to prevent bugs) - See https://gist.github.com/steipete/28849365e603dc2015c7107d85142e7b/revisions for a list of Xcode 8.3 changes
// clang -cc1 -analyzer-checker-help
// OVERVIEW: Clang Static Analyzer Checkers List
// USAGE: -analyzer-checker <CHECKER or PACKAGE,...>
//
// CHECKERS:
// alpha.core.BoolAssignment Warn about assigning non-{0,1} values to Boolean variables
// alpha.core.CallAndMessageUnInitRefArg
// Check for logical errors for function calls and Objective-C message expressions (e.g., uninitialized arguments, null function pointers, and pointer to undefined variables)
// alpha.core.CastSize Check when casting a malloc'ed type T, whether the size is a multiple of the size of T
// alpha.core.CastToStruct Check for cast from non-struct pointer to struct pointer