Skip to content

Instantly share code, notes, and snippets.

/// like XCTAssertEqual, but handles optional unwrapping
public func XCTAssertEqualOptional<T: Any where T: Equatable>(@autoclosure expression1: () -> T?, @autoclosure expression2: () -> T?, _ message: String? = nil, file: String = __FILE__, line: UInt = __LINE__) {
if let exp1 = expression1() {
if let exp2 = expression2() {
XCTAssertEqual(exp1, exp2, message ?? "", file: file, line: line)
} else {
XCTFail(message ?? "exp1 != nil, exp2 == nil", file: file, line: line)
}
} else if let exp2 = expression2() {
XCTFail(message ?? "exp1 == nil, exp2 != nil", file: file, line: line)
import Foundation
/// converts a string representing an unlocalized date with fixed-format to NSDate using Unix methods (suggested in Apple's Date Formatting guide: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html)
public func timeStringToDate(timeString: NSString, formatString: NSString) -> NSDate {
var t = tm()
return withUnsafeMutablePointer(&t) { t -> NSDate in
strptime_l(timeString.UTF8String, formatString.UTF8String, t, nil)
return NSDate(timeIntervalSince1970: NSTimeInterval(mktime(t)))
}
}
import XCPlayground
import CoreMIDI
XCPSetExecutionShouldContinueIndefinitely(true)
func notifyProc(a: UnsafePointer<MIDINotification>, b: UnsafeMutablePointer<Void>) -> Void {
let notification = a[0]
notification.messageID
notification.messageSize
}
@siemensikkema
siemensikkema / Themeable.swift
Created February 12, 2016 18:56
Themeable ViewController demonstration
import UIKit
protocol Themeable {
func applyTheme()
}
extension Themeable where Self: UIViewController {
func applyTheme() {
// beautiful!
navigationItem.leftBarButtonItem?.tintColor = .purpleColor()
// XCConfig file to enable all Swift compiler warnings
// Original by Jon Reid (see below).
// Modified by Siemen Sikkema: removed non-swift warnings
// XcodeWarnings by Jon Reid, http://qualitycoding.org/about/
// Source: https://github.com/jonreid/XcodeWarnings
// Apple LLVM 7.0 - Warning Policies
//GCC_TREAT_WARNINGS_AS_ERRORS = YES
@siemensikkema
siemensikkema / objcio S01E06 Multiple Cell Types.swift
Last active April 21, 2023 17:13
Multiple Cell Types example based on episode 6 of talk.objc.io in response to question in episode 9
import UIKit
import XCPlayground
struct Episode {
var title: String
}
struct Season {
var number: Int
@siemensikkema
siemensikkema / fixcodeproj.rb
Last active April 15, 2021 14:06
Post processing script for generated vapor Xcode project
#!/usr/bin/env ruby
require 'xcodeproj'
project_path = ARGV[0]
project = Xcodeproj::Project.open(project_path)
project.targets.each do |target|
# suppress warnings
if [
"Configs",
@siemensikkema
siemensikkema / Dockerfile
Last active May 4, 2019 14:42
Run Vapor in Docker with Redis and MySQL
FROM swift:4.2
ADD ./ /app
WORKDIR /app
RUN swift build -c release
CMD ["./wait-for-it.sh", "-t", "30", "database:3306", "--", "./.build/release/Run", "serve", "--hostname", "0.0.0.0"]
@siemensikkema
siemensikkema / Repositories.swift
Created October 8, 2019 10:21
Example of how we can use repositories in Vapor
import FluentMySQL
import Vapor
// MARK: Models
final class User: MySQLModel {
var id: Int?
var active: Bool
func createUser(request: Request) -> EventLoopFuture<HTTPResponseStatus> {
let password: String? = request.content["password"]
let email: String? = request.content["email"]
User.query(on: request.db).filter(\.email == email).count().map {
var validations = NewUser.validations()
validations.add("passwordAgain", as: String, is: .in(["password"])) // we need a .equals validator
if email != nil {
validations.add("email", result: UniqueEmailValidatorResult())