Skip to content

Instantly share code, notes, and snippets.

View AmitaiB's full-sized avatar

Amitai Blickstein AmitaiB

View GitHub Profile
@ollieatkinson
ollieatkinson / JSONObject.swift
Last active April 8, 2024 21:41
JSON Equatable in Swift
public let null = NSNull() as AnyHashable
public enum JSONObject {
case array([Any])
case dictionary([String: Any])
case fragment(Any)
}
extension JSONObject {
@inlinable public static func with(_ data: Data, options: JSONSerialization.ReadingOptions = []) throws -> JSONObject {
@phillippbertram
phillippbertram / RxCaptureMetadataOutputObjectsDelegateProxy.swift
Created January 16, 2018 10:39
RxSwift DelegateProxy for AVCaptureMetadataOutputObjectsDelegate
import AVFoundation
import RxSwift
import RxCocoa
extension Reactive where Base: AVCaptureMetadataOutput {
/// Reactive wrapper for `delegate`.
///
/// For more information take a look at `DelegateProxyType` protocol documentation.
public var delegate: DelegateProxy<AVCaptureMetadataOutput, AVCaptureMetadataOutputObjectsDelegate> {
@simonwuyts
simonwuyts / TooltipView.swift
Last active February 5, 2024 19:28
Customizable Tooltips
//
// TooltipView.swift
// Customizable Tooltips
//
// Copyright © 2017 Simon Wuyts
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@dabrahams
dabrahams / AnyComparable.swift
Created December 8, 2016 02:49
Faux Equatable and Comparable Existentials
class AnyEquatableBase {
func isEqual(to other: AnyEquatableBase) -> Bool {
fatalError("overrideMe")
}
}
class AnyEquatableBox<T: Equatable> : AnyEquatableBase {
let value: T
init(_ value: T) { self.value = value }
@taywils
taywils / JsonMinify.swift
Created October 17, 2016 04:07
Swift JSON Minify
import Foundation
extension String {
public var JSONminify: String {
// http://stackoverflow.com/questions/8913138/
// https://developer.apple.com/reference/foundation/nsregularexpression#//apple_ref/doc/uid/TP40009708-CH1-SW46
let minifyRegex = "(\"(?:[^\"\\\\]|\\\\.)*\")|\\s+"
if let regexMinify = try? NSRegularExpression(pattern: minifyRegex, options: .caseInsensitive) {
@AmitaiB
AmitaiB / OptionalLiteralType.swift
Created September 28, 2016 17:52
Provides failable initializers for X-Literals, e.g., `String(suspectedString) // returns a String optional`, just like `String(5) // returns a String, "5"`
/*
Instead of, say:
if let definitelyAString = perhapsAString as? String { // yadda... }
We now have:
String(perhapsAString) // returns a String? (an optional)
*/
protocol OptionalLiteralType {
@edenwaith
edenwaith / isModal.swift
Created April 4, 2016 20:52
Swift function to check if a view controller is being presented modally.
func isModal() -> Bool {
return self.presentingViewController?.presentedViewController == self
|| (self.navigationController != nil && self.navigationController?.presentingViewController?.presentedViewController == self.navigationController)
|| self.tabBarController?.presentingViewController is UITabBarController
}
@sketchytech
sketchytech / ButtonTooltip.swift
Last active March 1, 2023 14:18
Swift: Popover tooltip from button in iOS
import UIKit
class ViewController: UIViewController {
var label:ToolTip!
var labelTransform:CGAffineTransform!
let buttonHeight:CGFloat = 100
let buttonWidth:CGFloat = 200
override func viewDidLoad() {
class OverlayView: UIView {
weak var targetView: UIView?
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesBegan(touches, withEvent: event)
targetView?.touchesBegan(touches, withEvent: event)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesMoved(touches, withEvent: event)
@IanKeen
IanKeen / Example.swift
Last active July 10, 2019 22:02
Small utility methods to simplify dealing with Reusable items i.e. table/collection view cells
//`UITableViewCell` and `UICollectionViewCell` are `Reusable` by defaut
//Use the extension method to dequeue an instance of the appropriate `Reusable`
class MyVC: UITableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
tableView
.registerReusable(FooCell.self)
.registerReusable(BarCell.self)
}