Skip to content

Instantly share code, notes, and snippets.

View TheCodedSelf's full-sized avatar

Keegan Rush TheCodedSelf

View GitHub Profile
@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)
@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
@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 / 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)
@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 / 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 / RxSwiftChainObservablesExample.swift
Last active June 29, 2018 19:36
RxSwift: An example of causing one observable to rely on the output of another
import RxSwift
func submit(number: Int) {
let validateAndPerformServiceCallIfSuccessful = performValidation(numberThatShouldBeEven: number)
.flatMap { () -> Observable<Void> in
print("Validated successfully")
return performServiceCall() // For any .next events from performValidation, perform the service call
}
// By subscribing, validation is performed and the service call is executed for any .next events from performValidation
@TheCodedSelf
TheCodedSelf / ImageStore.swift
Created April 29, 2017 06:05
Store, retrieve, and delete images in your iOS app's local storage
import UIKit
struct ImageStore {
static func delete(imageNamed name: String) {
guard let imagePath = ImageStore.path(for: name) else {
return
}
try? FileManager.default.removeItem(at: imagePath)
@TheCodedSelf
TheCodedSelf / author-amend.sh
Created February 12, 2017 17:19
Change all commits with author email "bruce.wayne@wayneenterprises.com" to "batman@justiceleague.com"
#!/bin/sh
git filter-branch --env-filter '
OLD_EMAIL="bruce.wayne@wayneenterprises.com"
CORRECT_NAME="The Dark Knight"
CORRECT_EMAIL="batman@justiceleague.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
@TheCodedSelf
TheCodedSelf / MainTryCatch.m
Last active October 18, 2016 09:00
Wrap Objective-C Main method in a try-catch block for exception handling
int main(int argc, char *argv[]) {
@autoreleasepool {
@try {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
} @catch (NSException *exception) {
#if DEBUG
NSLog(@"\n\n********************");
NSLog(@"UNHANDLED EXCEPTION:");
NSLog(@"%@", exception);
NSLog(@"Stack trace: %@", [exception callStackSymbols]);