Skip to content

Instantly share code, notes, and snippets.

@TheCodedSelf
TheCodedSelf / RxSwiftBindToButton.swift
Created May 13, 2017 17:16
RxSwift: Bind to button taps
import UIKit
import RxSwift
import RxCocoa
let button = UIButton()
button.rx.tap.bind {
print("button tapped")
}
@TheCodedSelf
TheCodedSelf / RxSwiftMerge.swift
Created May 13, 2017 17:28
RxSwift - Merge the results of multiple Observables
import Foundation
import RxSwift
let cars = Observable.from(["Nissan", "Ford", "BMW"])
let tanks = Observable.from(["Tiger II", "T-44", "Panther"])
let bikes = Observable.from(["Ducati", "Honda", "Kawasaki"])
let landVehicles = Observable.of(cars, tanks, bikes).merge()
landVehicles.subscribe(onNext: { print($0) })
@TheCodedSelf
TheCodedSelf / ImagePickerExample.swift
Created June 30, 2017 06:03
Basic example of using UIImagePickerController
class ViewController: UIViewController {
func pickImage(fromSource sourceType: UIImagePickerControllerSourceType) {
guard UIImagePickerController.isSourceTypeAvailable(sourceType) else { return }
let imagePickerController = UIImagePickerController()
imagePickerController.sourceType = sourceType
imagePickerController.delegate = self
present(imagePickerController, animated: true, completion: nil)
@IBAction private func startToastAnimation(_ sender: Any) {
let toastView = createToastView()
view.addSubview(toastView)
animate(toastView: toastView)
}
private func createToastView() -> UIView {
// 1.
let toastViewHeight = CGFloat(80)
@TheCodedSelf
TheCodedSelf / find_emails.py
Created April 10, 2018 16:25
Scrape a web page for any email addresses
import urllib2, re
def find_email(url):
hdr = {'User-Agent': 'Mozilla/5.0'}
req = urllib2.Request(url, headers=hdr)
try:
f = urllib2.urlopen(req)
s = f.read()
emails = re.findall(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}",s)
return emails
@TheCodedSelf
TheCodedSelf / ActionButton.swift
Last active July 17, 2018 05:00
Create a macOS Action (Gear) Button Programmatically (www.thecodedself.com/macOS-action-button-swift/)
let actionButton = NSPopUpButton(frame: .zero, pullsDown: true)
view.addSubview(actionButton)
actionButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
actionButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
actionButton.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])
["Option 1", "Option 2", "Option 3"].forEach(actionButton.addItem)