Skip to content

Instantly share code, notes, and snippets.

@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)
@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 / 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)")
}
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: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

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:

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()
}
@jmnavarro
jmnavarro / UIActivityIndicatorView+Concurrent
Created June 24, 2015 09:25
Closes the UIActivityIndicator used by concurrent operations when the last one finishes
/*
* Handles the scenario when you have concurrent operations using
* the same activity indicator. Without care, this leads to stop the
* indicator when the first operation ends:
* Start A
* Start B
* Start C
* Finish B -> without this extensions, the indicator will be stopped here
* Finish C
* Finish A -> with this extensions, the indicator will be stopped here
@jmnavarro
jmnavarro / ProtocolBasedCellFactory.swift
Created October 27, 2015 15:43
Protocol based UITableView
// This is the link between the model and the view
// Mascot <-> MascotTableViewCell
extension CollectionType where Generator.Element == Employee {
func elementByPosition(position: Int) -> Self.Generator.Element? {
// dirty hack. It's already implemented in Array subscript with O(1) instead of O(n)
var i = 0
for e in self {
if i++ == position {
return e
@jmnavarro
jmnavarro / flatmap-magic.swift
Last active May 31, 2016 16:23
FlatMap magic with optionals
var number: Int?
number.flatMap {
// this is never executed
print($0)
}
number = 1
number.flatMap {