Skip to content

Instantly share code, notes, and snippets.

View jarsen's full-sized avatar

Jason Larsen jarsen

View GitHub Profile
@jarsen
jarsen / tricks.swift
Last active January 16, 2016 14:17
Swift Tricks
// also see http://oleb.net/blog/2014/07/swift-strings/
// Mark: Strings
// if you want to stringByReplacingOccurrencesOfString without using a nil range, you need to convert to NSString first
var str = "some junk"
let tmpString: NSString = str
str = tmpString.stringByReplacingOccurrencesOfString(" ", withString: "+", options: nil, range: NSMakeRange(1, 6))
// find the length of a swift string
# 1) open irb
# 2) `load "code_knight.rb"`
# 3) `knights = Marshal::load(Marshal::dump($knights))`
# 4) do the problems using your variable `knights`
# armor types - :light, :medium, :heavy
# ethical_alignment - :chaotic, :neutral, :lawful
# moral_alignment - :good, :neutral, :evil
class CodeKnight
# TOP SECRET
# CodeNight CodeKnight Answer Key!!!
# Problem 0
# Use Enumerable.each to print out a string for each knight in the format of "Jason prefers a sword"
knights.each {|knight| puts "#{knight.name} prefers a #{knight.preferred_weapon}"}
# Problem 1
# Use Enumerable.find_all to find all knights with 50 or more health
# example of incrementing all elements across a list
Enum.map [1,2,3], fn(x) -> x + 1 end
Enum.map [1,2,3], &1 + 1
@jarsen
jarsen / example.rkt
Last active December 19, 2015 05:39
(module example racket
(define (my_print str)
(print str)))
(my_print "Hello, World!")
@jarsen
jarsen / example.exs
Last active December 19, 2015 05:39
defmodule Example do
def my_puts str do
IO.puts str
end
end
Example.my_puts "Hello, World!"
// old way
- (NSMutableArray *)sections {
if (!_sections) {
_sections = [NSMutableArray new];
}
return _sections;
}
// cool new way
- (NSMutableArray *)sections {
protocol StoryboardInstantiable {
static var storyboardName: String { get }
static var storyboardIdentifier: String { get }
}
extension StoryboardInstantiable {
static func instantiateFromStoryboard() -> Self {
let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
guard let vc = storyboard.instantiateViewControllerWithIdentifier(storyboardIdentifier) as? Self else {
fatalError("Instantiated view controller does not match type")
@jarsen
jarsen / KeyboardNotifications.swift
Created July 16, 2015 17:14
keyboard notifications
func keyboardWillShow(notification: NSNotification) {
print("Showing keyboard")
guard let userInfo = notification.userInfo,
rectValue = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue,
rawCurve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? UInt,
duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? Double else {
return
}
let keyboardFrame = rectValue.CGRectValue()
import UIKit
enum MyError: ErrorType {
case Oooggh(String)
}
func errorProne() throws {
throw MyError.Oooggh("nope")
}