Skip to content

Instantly share code, notes, and snippets.

@rickharrison
rickharrison / UIViewController.m
Created August 16, 2013 05:56
iOS 7 Messages Gradient Experimentation
//
// NRTRootViewController.m
// GradientTableView
//
// Created by Rick Harrison on 8/15/13.
// Copyright (c) 2013 Rick Harrison. All rights reserved.
//
#import "NRTRootViewController.h"
@seanlilmateus
seanlilmateus / channel.swift
Last active November 22, 2017 14:02
swift Go like channels
import Foundation
// Playground - noun: a place where people can play
class Channel<T> {
var stream: Array<T>
let queue: dispatch_queue_t
let semaphore: dispatch_semaphore_t
init() {
self.stream = []
@jverkoey
jverkoey / NSManagedObjectContext+DebugSwizzling.m
Created April 14, 2014 11:52
Core Data Managed Object Context Debugging
// NSManagedObjectContext+DebugSwizzling.h
#import <CoreData/CoreData.h>
#if DEBUG
/**
* Toggles debugging of Core Data managed object contexts.
*
* When enabled, will fire NSLogs in the following cases:
@jakubpetrik
jakubpetrik / handle-signal.swift
Created September 23, 2015 21:34
Signal handling in Swift
/**
http://rosettacode.org/wiki/Handle_a_signal
Most general purpose operating systems provide interrupt facilities, sometimes called signals.
Unhandled signals generally terminate a program in a disorderly manner. Signal handlers are
created so that the program behaves in a well-defined manner upon receipt of a signal.
For this task you will provide a program that displays a single integer on each line of output at
the rate of one integer in each half second. Upon receipt of the SigInt signal (often created by the user typing ctrl-C)
the program will cease printing integers to its output, print the number of seconds the program has run,
@JadenGeller
JadenGeller / AnySet.swift
Created March 30, 2015 22:13
Swift Set of "Any" Type
// Examples
var set = AnySet()
set.insert(3)
set.insert("hello")
set.insert("hi")
set.insert(2.3)
set.contains(6) // -> false
set.contains(3) // -> true
@jamiew
jamiew / mp4box_hacks.sh
Created March 7, 2013 18:56
Adding subtitles and audio tracks to an MP4 video with MP4Box
# Add subtitle track
MP4Box -add minecraft_uncut.srt:lang=eng:layout=0x60x0x-1:group=2:hdlr="sbtl:tx3g" Minecraft2_mobile.mp4
# Add audio track
# 1. doesnt work in quicktime/itunes
MP4Box -add commentary_audio.aac Minecraft2_mobile.mp4
# 2. supposed to work in quicktime/itunes but doesn't seem to; :name at least is definitely not supported
MP4Box -add commentary_audio.aac:disable:group1:lang=en:name="Director's Commentary" Minecraft2_mobile.mp4
@dabrahams
dabrahams / ModuleRepl.md
Last active May 18, 2021 00:16
Linking and importing a swift module for use in the REPL
$ echo 'public func f() { print("YES") }' > x.swift
$ swiftc -emit-library -emit-module -module-link-name x  x.swift
$ swift -L . -I .
Welcome to Apple Swift version 5.4 (swiftlang-1205.0.26.9 clang-1205.0.19.55).
Type :help for assistance.
  1> import x
import x
  2> f()
f()
@DougGregor
DougGregor / dynamic_member_lookup_environment.swift
Created May 2, 2018 16:59
Using Swift 4.2's @dynamicMemberLookup to expose environment variables
import Darwin
@dynamicMemberLookup
struct Environment {
subscript(dynamicMember name: String) -> String? {
get {
guard let value = getenv(name) else { return nil }
return String(validatingUTF8: value)
}
@krzyzanowskim
krzyzanowskim / AsyncWaiter.swift
Last active June 25, 2022 12:25
Synchronously (well) wait for async Task value update https://twitter.com/krzyzanowskim/status/1523233140914876416
/// Wait for async operation to return value and call callback with the value
/// This class is intended to workaround/simplify async/await + actors isolation
/// https://twitter.com/krzyzanowskim/status/1523233140914876416
private class AsyncWaiter<T> {
var didReceiveValue: Bool = false
let value: (T) -> Void
let operation: () async throws -> T
init(_ value: @escaping (T) -> Void, operation: @escaping () async throws -> T) {
self.value = value
import UIKit
extension UIResponder {
private weak static var _currentFirstResponder: UIResponder? = nil
public static var current: UIResponder? {
UIResponder._currentFirstResponder = nil
UIApplication.shared.sendAction(#selector(findFirstResponder(sender:)), to: nil, from: nil, for: nil)
return UIResponder._currentFirstResponder
}