Skip to content

Instantly share code, notes, and snippets.

@yannxou
yannxou / AsyncTasksJoin.m
Created October 18, 2016 14:24
Perform various asynchronous tasks and wait for all results
// Perform various asynchronous tasks and wait for all results
__block NSObject *result1;
__block NSObject *result2;
__block NSObject *result3;
dispatch_group_t dispatchGroup = dispatch_group_create();
dispatch_group_enter(dispatchGroup);
[self performBackgroundTask:^(NSObject *result) {
@yannxou
yannxou / Optional+Utils.swift
Last active March 20, 2018 14:31
Optional(String) isNilOrEmpty helper method
extension Optional where Wrapped == String {
var isNilOrEmpty: Bool {
return (self ?? "").isEmpty
}
}
@yannxou
yannxou / CustomAssertion.swift
Created August 10, 2018 09:25
Custom assertions
// Xcode will notify in the appropiate line when the custom assertion function fails
func assertSomething(with param: InterestingObject, file: StaticString = #file, line: UInt = #line) {
XCTFail("Bad bad", file: file, line: line)
}
@yannxou
yannxou / OptionalMatching.swift
Created October 3, 2018 13:22
Formatting an optional value
func description(for percentage: Int?) -> String {
switch percentage {
case .some(0):
return "Minimum value"
case .some(let value):
return "Value: \(value)"
case .none:
return "Value not specified"
}
}
@yannxou
yannxou / .bash_profile
Created October 17, 2018 07:33
Bash profile with colors and git branch name
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
# Appearance
# PS1="\h:\W \u\$" # default
export PS1="\u@\h \[\033[32m\]\W\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "
export CLICOLOR=1
export LSCOLORS=ExFxBxDxCxegedabagacad
@yannxou
yannxou / FloatCast.swift
Created January 21, 2019 13:52
Float cast to nil (bug?)
import Foundation
import UIKit
func swiftVersion() -> String {
#if swift(>=4.2.1)
return "4.2.1"
#elseif swift(>=4.2)
return "4.2"
#elseif swift(>=4.1)
return "4.1"
@yannxou
yannxou / ArrayCaseIteration.swift
Created January 23, 2019 14:59
Iterate on array while filtering
let array: [Any] = ["String1", 2, "String2", 3, 4]
for case let str as String in array {
print(str)
}
@yannxou
yannxou / gist:1ad24641020a7c1260d4d6bc9404f6c9
Created February 5, 2019 16:20
GIT: Reset last commit
# Reset last commit (if changes have not been pushed yet)
git reset --soft HEAD~1
@yannxou
yannxou / arrayJoin.swift
Created February 7, 2019 15:36
Join array of optional strings (the functional way)
struct Data {
let name: String?
let location: String?
var components: String {
return [name, location]
.compactMap { $0 }
.filter { !$0.isEmpty }
.joined(separator: " - ")
}
@yannxou
yannxou / PatternMatching.swift
Created February 13, 2019 16:37
Pattern Matching with optionals
switch john.residence?.numberOfRooms {
case .Some(let x) where x % 2 == 0:
println("Even number of rooms")
default:
println("Odd number of rooms")
}