Skip to content

Instantly share code, notes, and snippets.

@sketchytech
sketchytech / paypal_state_and_province_codes.json
Created September 4, 2018 19:57
PayPal State and Province Codes
[{
"Argentina": [{
"province": "Buenos Aires (Ciudad)",
"code": "CIUDAD AUTÓNOMA DE BUENOS AIRES"
}, {
"province": "Buenos Aires (Provincia)",
"code": "BUENOS AIRES"
}, {
"province": "Catamarca",
"code": "CATAMARCA"
@sketchytech
sketchytech / paypal_country_codes.json
Last active September 4, 2018 19:57
PayPal Country Codes
[{
"ALBANIA": {
"code": "AL"
}
},
{
"ALGERIA": {
"code": "DZ"
}
},
@sketchytech
sketchytech / Date_Extensions
Last active May 29, 2018 14:55
Playing with Date, DateInterval, DateComponents, Calendar
extension Date {
init?(iso:String) {
let d = ISO8601DateFormatter()
guard let date = d.date(from: iso) else {
return nil
}
self = date
}
func year() -> String {
@sketchytech
sketchytech / DragZoomPinch.swift
Last active August 28, 2023 23:11
Using Auto Layout Constraints for Drag, Pinch, Zoom and Tap
import UIKit
import PlaygroundSupport
class ViewController: UIViewController {
var redView:UIView!
var movedRight:Bool = false
var verticalConstraint:NSLayoutConstraint!
var horizontalConstraint:NSLayoutConstraint!
var widthConstraint:NSLayoutConstraint!
var heightConstraint:NSLayoutConstraint!
@sketchytech
sketchytech / KeychainService.swift
Created June 7, 2017 10:16
Simple password storage, update, retrieval and deletion in Keychain macOS
import Cocoa
import Security
// Adaptation of https://stackoverflow.com/a/37539998/1694526
// Arguments for the keychain queries
let kSecClassValue = NSString(format: kSecClass)
let kSecAttrAccountValue = NSString(format: kSecAttrAccount)
let kSecValueDataValue = NSString(format: kSecValueData)
let kSecClassGenericPasswordValue = NSString(format: kSecClassGenericPassword)
let kSecAttrServiceValue = NSString(format: kSecAttrService)
@sketchytech
sketchytech / FileManager_Extensions.swift
Last active May 5, 2022 22:33
FileManager: Replace with copy of file
extension FileManager {
func replaceWithCopyOfFile(at:URL, with:URL) {
do {
let url = try self.url(for: .itemReplacementDirectory, in: .userDomainMask, appropriateFor: with.deletingPathExtension(), create: true)
try self.copyItem(at: with, to: url.appendingPathComponent(with.lastPathComponent))
let alert = NSAlert()
alert.messageText = "Replace \"\(at.lastPathComponent)\" in \"\(at.pathComponents[at.pathComponents.count - 2])\" with new file?"
alert.addButton(withTitle: "OK")
alert.addButton(withTitle: "Cancel")
@sketchytech
sketchytech / PDFSearch.swift
Created February 22, 2017 16:20
Swift 3: PDF text search on macOS returns page labels (numbers) as strings
import Cocoa
import Quartz
enum SearchType: Int {
case CaseInsensitiveSearch = 1, LiteralSearch, BackwardsSearch, AnchoredSearch, NumericSearch
}
extension PDFDocument {
func pages(withString str:String, searchType type:SearchType) -> [String] {
let selec = self.findString(str, withOptions: type.rawValue)
@sketchytech
sketchytech / AnimationPlayground.swift
Created January 14, 2017 12:12 — forked from crenwick/AnimationPlayground.swift
Swift 3: Draw a path with a semicircle, animation movement across it.
// Follow the horizon
import UIKit
import PlaygroundSupport
import SpriteKit
let containerView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 375.0, height: 300.0))
let view = SKView(frame: CGRect(x:0, y:0, width:375, height:120))
PlaygroundPage.current.liveView = view
@sketchytech
sketchytech / TimeDate.swift
Created November 14, 2016 10:11
Swift 3: Time and Date
import UIKit
// see here for details about date formatter https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369
func time()->(h:Int,m:Int,s:Int) {
let formatStrings = ["hh","mm","ss"]
var hms = [Int]()
let dateFormatter = DateFormatter()
let date = Date()
@sketchytech
sketchytech / StableSort.swift
Last active October 3, 2016 12:48
Airspeed Velocity's Stable Sort updated to Swift 3
// Swift 2 stable sort taken from AirspeedVelocity blog and updated to Swift 3
// http://airspeedvelocity.net/2016/01/10/writing-a-generic-stable-sort/
extension RangeReplaceableCollection
where
Index: Strideable,
SubSequence.Iterator.Element == Iterator.Element,
IndexDistance == Index.Stride {