Skip to content

Instantly share code, notes, and snippets.

View vjosullivan's full-sized avatar

vjosullivan vjosullivan

  • Reading, England
View GitHub Profile
@vjosullivan
vjosullivan / CoalescingOperator.swift
Created January 2, 2017 10:17
??? - A custom optional -> string coalescing operator
// Source: https://oleb.net/blog/2016/12/optionals-string-interpolation
infix operator ???: NilCoalescingPrecedence
/// Provides a String default value for an Optional of any type.
/// Overcomes the problem with the `??` operator whare the default value must be
/// of the same type as the left hand side.
public func ???<T>(optional: T?, defaultValue: @autoclosure () -> String) -> String {
switch optional {
case let value?: return String(describing: value)
@vjosullivan
vjosullivan / testData.swift
Created August 26, 2016 09:54
Swift 3. Creates an array of n integers with random values in the range 0..<max. As written below, the code will generates 10 integers in the range 0..<100.
let testData = { (n: Int, max: UInt32) -> [Int] in
var array = [Int]()
for i in 0..<n {
array.append(Int(arc4random_uniform(max)))
}
return array
}(10, 100)
@vjosullivan
vjosullivan / SortingTemplate.swift
Last active August 26, 2016 09:38
A Swift 3.0 template for comparing sorting algorithms.
import Foundation
/// A template for comparing different sorting algorithms.
///
/// To use:
/// - Sublass `SortTemplate`.
/// - Override the function `algorithm to sort the internal var `array`.
/// - Initialise the new class with data to be sorted.
/// - Call the `sort()` function.
class SortTemplate<C: Comparable> {
@vjosullivan
vjosullivan / deviceDetails.swift
Created April 16, 2015 09:05
Some device details (requires iOS 8.0.0 or later).
import Foundation
// Is the device's iOS version equal or later to the targeted version?
let targetOSVersion = NSOperatingSystemVersion(majorVersion:8, minorVersion:0, patchVersion:0)
if NSProcessInfo().isOperatingSystemAtLeastVersion(targetOSVersion) {
println("Running iOS version 8.0.0 or later.")
} else {
println("Running iOS version 7 or earlier.")
}
@vjosullivan
vjosullivan / PreferredFonts.swift
Last active August 29, 2015 14:19
Something learnt from reading another person's code...
titleLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
bodyLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleBody)
footnoteLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleFootnote)
//
// TableViewController.swift
// ColouredRose2
//
// Created by Vincent O'Sullivan on 08/04/2015.
// Copyright (c) 2015 Vincent O'Sullivan. All rights reserved.
//
import UIKit
func getDatedString(basicDictionary: NSMutableArray, selectedIndex: Int) -> String {
return basicDictionary.objectAtIndex(selectedIndex)
}
- (NSString *) getDatedString:(NSMutableArray *)basicDictionary forindex:(int)selectedIndex {
return ([basicDictionary objectAtIndex:selectedIndex]);
}
@vjosullivan
vjosullivan / 0_reuse_code.js
Last active August 29, 2015 14:14
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console