Skip to content

Instantly share code, notes, and snippets.

func given(givenText: String, givenCode: ()->(),
when whenText: String, whenCode: ()->(),
then thenText: String, thenCode: ()->()) {
println("\n\n=> SCENARIO\n\tGiven '\(givenText)'\n\tWhen '\(whenText)'\n\tThen '\(thenText)'")
givenCode()
whenCode()
thenCode()
}

Keybase proof

I hereby claim:

  • I am jmnavarro on github.
  • I am jmnavarro (https://keybase.io/jmnavarro) on keybase.
  • I have a public key whose fingerprint is 72FB 7F2A 24BD EEC9 0BCD 5C6A 3399 DBAA D1E2 E912

To claim this, I am signing this object:

@jmnavarro
jmnavarro / gist:daf887bf288038d52e79
Created September 30, 2014 15:18
Real life map+reduce solution
//
// returns the list of different extensions from a bunch of files
//
func differentExtensions(fileNames: [String]) {
// Step 1. Map: process file names list to convert to extensions list
let allExtensions = fileNames.map { $0.pathExtension.lowercaseString }
// Step 2. Reduce: iterate removing duplicates
return allExtensions.reduce([String]()) { (acum, value) -> [String] in
extension Bool : StringLiteralConvertible {
static public func convertFromStringLiteral(value: StringLiteralType) -> Bool {
return (value.lowercaseString == "false") ? false : true
}
static public func convertFromExtendedGraphemeClusterLiteral(value: StringLiteralType) -> Bool {
return convertFromStringLiteral(value)
}
@jmnavarro
jmnavarro / gist:25c6c003d8f2ecee4be8
Last active April 27, 2016 04:49
Since it seems XCTAssertNotNil is not working with Swift's optional, this is an alternative method. In order to stop execution when test fails, check XCTestCase's continueAfterFailure property
func XCTAssertOptional(expression: @autoclosure () -> AnyObject?, _ message: String? = nil) {
let evaluatedExpression:AnyObject? = expression()
if evaluatedExpression == nil {
if let messageValue = message {
XCTFail(messageValue)
}
else {
XCTFail("Optional asertion failed: \(evaluatedExpression)")
}
@jmnavarro
jmnavarro / what_the_hell_is_doing_this_image_here
Created February 21, 2013 11:11
Your XCode project uses a lot of images. Maybe some of them are not used anymore, but which ones?
cd ./Images
for f in *.png; do
fn=`echo "$f" | cut -d'.' -f1`
if ! grep -q @2x <<< $fn; then
if ! $(grep -i -r --include=*.m --include=*.xib "`echo "$f" | cut -d'.' -f1`" ../Classes/ > /dev/null); then
echo $f
fi
fi
done
@jmnavarro
jmnavarro / NSProcessInfo+DetectTestEnvironment.h
Created January 31, 2013 11:59
Determine if host app is running in test environment or in real one. Usefull to use in applicationDidFinishLaunching to change the startup path.
//
// NSProcessInfo+DetectTestEnvironment.h
// Memoir
//
// Created by JM - http://jmnavarro.github.com
//
#import <Foundation/Foundation.h>
@interface NSProcessInfo (DetectTestEnvironment)