Skip to content

Instantly share code, notes, and snippets.

View BasThomas's full-sized avatar

Bas Broek BasThomas

View GitHub Profile
//
// Extensions.swift
// Apostle Partnerapp
//
// Created by Bas on 15/01/2015.
// Copyright (c) 2015 Bas. All rights reserved.
//
import UIKit
@BasThomas
BasThomas / soonestDate
Last active August 29, 2015 14:16
Get the soonest date from a list of dates
extension NSDate
{
/**
Checks if the date is in the past.
:returns: If date is passed or not
*/
func inPast() -> Bool
{
let now = NSDate()
@BasThomas
BasThomas / Sorting
Created April 19, 2015 18:31
Sorting
private static List<Map.Entry<String, Integer>> sortByValue(Map<String, Integer> unsortMap)
{
List<Map.Entry<String, Integer>> list = new LinkedList<>(unsortMap.entrySet());
// Sorting the list based on values
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>()
{
@Override
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2)
/**
Filters the list of clients according to the query.
:param: searchText The query.
*/
func filterContentForSearchText(searchText: String)
{
self.filteredClients = self.clients.filter(
{
(client: Client) -> Bool in
import Foundation
struct Message {
var timestamp: Double
}
extension Message: Printable {
var description: String {
return "Message with timestamp \(self.timestamp)"
}
@BasThomas
BasThomas / Enum.swift
Last active August 29, 2015 14:24
Enums in Xcode 7 beta 3
import Foundation
enum Number: Int {
case Zero
case One
case Two
case Three
}
enum NumberString: String {
@BasThomas
BasThomas / LowercaseInitializable.swift
Created July 25, 2015 08:39
LowercaseInitializable
public protocol LowercaseInitializable {
static var allValues: [Self] { get }
init?(rawValue: String)
}
extension LowercaseInitializable {
public init?(rawValue: String) {
import Foundation
let jsonString = "{\"message\": \"422 Unprocessable Entity\",\"errors\": {\"email\": [\"The email has already been taken.\"],\"mobile_number\" : [\"The mobile number has already been taken.\"]}}"
let jsonData = jsonString.data(using: .utf8)
do {
if let jsonData = jsonData {
let object = try JSONSerialization.jsonObject(with: jsonData, options: []) as? [AnyHashable: Any]
}
import Foundation
URL(string: "https://en.wikipedia.org/wiki/Flesch–Kincaid_readability_tests#Flesch_reading_ease") // nil
URL(string: "https://en.wikipedia.org/wiki/Flesch-Kincaid_readability_tests#Flesch_reading_ease") // valid
URL(string: "https://en.wikipedia.org/wiki/Flesch–Kincaid_readability_tests#Flesch.E2.80.93Kincaid_grade_level") // nil
URL(string: "https://en.wikipedia.org/wiki/Flesch-Kincaid_readability_tests#Flesch.E2.80.93Kincaid_grade_level") // valid
"https://en.wikipedia.org/wiki/Flesch–Kincaid_readability_tests#Flesch_reading_ease" ==
"https://en.wikipedia.org/wiki/Flesch-Kincaid_readability_tests#Flesch_reading_ease" // false
@BasThomas
BasThomas / matrix.swift
Last active November 24, 2017 14:54
Switching on optional types
let o1nil: Int? = nil
let o1: Int? = 1
func switcher(_ a: Int?, _ b: Int?) {
switch (a, b) {
case (nil, nil):
print("nothing")
case (let thing?, nil):
print("lhs", thing)
case (nil, let thing?):