This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(module example racket | |
(define (my_print str) | |
(print str))) | |
(my_print "Hello, World!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Example do | |
def my_puts str do | |
IO.puts str | |
end | |
end | |
Example.my_puts "Hello, World!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// old way | |
- (NSMutableArray *)sections { | |
if (!_sections) { | |
_sections = [NSMutableArray new]; | |
} | |
return _sections; | |
} | |
// cool new way | |
- (NSMutableArray *)sections { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
enum MyError: ErrorType { | |
case Oooggh(String) | |
} | |
func errorProne() throws { | |
throw MyError.Oooggh("nope") | |
} |