Skip to content

Instantly share code, notes, and snippets.

View TheCodedSelf's full-sized avatar

Keegan Rush TheCodedSelf

View GitHub Profile
@TheCodedSelf
TheCodedSelf / NavigationBars.swift
Last active January 19, 2016 17:52
Playing with UINavigationBars
// Set title text
self.navigationItem.title = "Title"
// Set title text color
let desiredColor = UIColor.whiteColor()
navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : desiredColor]
// Set back bar button item title
let backButton = UIBarButtonItem(title: "Back", style: .Plain, target: nil, action: nil)
self.navigationItem.backBarButtonItem = backButton
@TheCodedSelf
TheCodedSelf / Misc.swift
Last active January 23, 2016 10:22
Miscellaneous
// Random number between 0 and n
let n = 42
let randomNumber = Int(arc4random_uniform(n))
// Instantiate a view controller that exists in a storyboard
let storyboardName = "Main"
let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
let storyboardIdForViewController = "MyVC" // Specified in IB on the specific view controller instance
let myVc: MyViewController = storyboard.instantiateViewControllerWithIdentifier(storyboardIdForViewController) as! MyViewController
@TheCodedSelf
TheCodedSelf / xopen
Last active September 8, 2016 05:19
Bash script that opens any xcworkspace or xcodeproj found in the current directory in Xcode
#!/bin/bash
#
# Opens any xcworkspace or xcodeproj found in the current directory in Xcode.
#
openfile () (
file="$1"
echo "Opening $file";
open $file;
)
@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]);
import UIKit
extension UIView
{
func shake()
{
let shakeAngle = (5 * M_PI) / 360
let shakeRotation = CAKeyframeAnimation(keyPath: "transform.rotation")
@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 / 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 / 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 / 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)
@IBAction private func startToastAnimation(_ sender: Any) {
let toastView = createToastView()
view.addSubview(toastView)
animate(toastView: toastView)
}
private func createToastView() -> UIView {
// 1.
let toastViewHeight = CGFloat(80)