Skip to content

Instantly share code, notes, and snippets.

View mjarvis's full-sized avatar

Malcolm Jarvis mjarvis

  • British Columbia
View GitHub Profile
@adamgit
adamgit / .gitignore
Last active July 17, 2024 12:58
.gitignore file for Xcode4 / OS X Source projects
#########################
# .gitignore file for Xcode4 and Xcode5 Source projects
#
# Apple bugs, waiting for Apple to fix/respond:
#
# 15564624 - what does the xccheckout file in Xcode5 do? Where's the documentation?
#
# Version 2.6
# For latest version, see: http://stackoverflow.com/questions/49478/git-ignore-file-for-xcode-projects
#
@mjarvis
mjarvis / NSHTMLTextDocumentType
Last active October 16, 2017 22:05
This GIST is to help explain the behaviour of NSAttributedString's "initWithData:options:documentAttributes:error:" when used with NSHTMLDocumentType.At first glance, one would expect the output here to be "1, 2, 3", But after some careful reading of the documentation, and if you run the code here, we find that not to be true.
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"3");
});
NSLog(@"1");
[[NSAttributedString alloc] initWithData:[@"<span>html!</span>" dataUsingEncoding:NSUTF8StringEncoding]
options:@{
@loganwright
loganwright / PrioritySort.swift
Created October 22, 2015 20:16
Sort Priorities in Swift
enum SortOrder {
case Ascending, Descending, Same
}
extension Array {
typealias Sorter = (Element, Element) -> SortOrder
func sortWithPriorities(sorters: Sorter...) -> Array {
return sort { (left, right) -> Bool in
for sorter in sorters {
switch sorter(left, right) {
@Sorix
Sorix / ColorableNavigationController.swift
Created April 12, 2017 14:13
Colourable UINavigationController that supports different colors for navigation bars among different view controllers
//
// ColorableNavigationController.swift
//
// Created by Vasily Ulianov on 26.10.16.
//
import UIKit
/// Navigation bar colors for `ColorableNavigationController`, called on `push` & `pop` actions
public protocol NavigationBarColorable: class {
var navigationTintColor: UIColor? { get }
@IanKeen
IanKeen / EncodableDictionary.swift
Last active April 13, 2019 18:37
Type-safe Encodable Dictionary - useful for building quick dynamic Encodable packets
public struct EncodableDictionary {
typealias EncodingFunction = (inout KeyedEncodingContainer<AnyCodingKey>) throws -> Void
// MARK: - Private Properties
private var data: [String: Any] = [:]
private var encodings: [String: EncodingFunction] = [:]
// MARK: - Lifecycle
public init() { }
@IanKeen
IanKeen / NotificationCenter+TypeSafe.swift
Last active December 7, 2022 21:31
Type safe NotificationCenter
struct Notification<T> {
let name: NSNotification.Name
}
private let notificationData = "_notificationData"
extension NotificationCenter {
func post<T>(_ notification: Notification<T>, object: Any? = nil, data: T) {
post(name: notification.name, object: object, userInfo: [notificationData: data])
}
@IanKeen
IanKeen / AnyCodable.swift
Last active March 4, 2021 18:45
AnyCodable
public struct AnyCodable: Codable {
public let value: Any?
public init(_ value: Any?) {
self.value = value
}
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
@IanKeen
IanKeen / AnyCodingKey.swift
Created March 1, 2019 18:40
AnyCodingKey: Helpful for a range of Codable tricks
struct AnyCodingKey: CodingKey {
var stringValue: String
var intValue: Int?
init?(intValue: Int) {
self.intValue = intValue
self.stringValue = "\(intValue)"
}
init?(stringValue: String) {
self.intValue = nil
@IanKeen
IanKeen / DictionaryDecoder.swift
Created March 1, 2019 18:43
DictionaryDecoder
import Foundation
class DictionaryDecoder {
init() { }
func decode<T: Decodable>(_ type: T.Type, from data: [String: Any]) throws -> T {
let decoder = _Decoder(codingPath: [], source: data)
return try T(from: decoder)
}
}
@wtsnz
wtsnz / ContentView.swift
Last active July 5, 2024 15:41
Playing around with hosting SwiftUI Views in an NSWindow from inside a View 🙃 (also works for the NSStatusBar item!)
import SwiftUI
struct ContentView: View {
@State var now = Date()
@State var showWindow = false
@State var text: String = ""
let timer = Timer.publish(every: 1, on: .current, in: .common).autoconnect()