Skip to content

Instantly share code, notes, and snippets.

bwhiteley / HostingView.swift
Last active Nov 5, 2022
Host SwiftUI in a UIView
View HostingView.swift
import Foundation
import SwiftUI
#if os(macOS)
public typealias PlatformViewType = NSView
#elseif !os(watchOS)
import UIKit
public typealias PlatformViewType = UIView
#if !os(watchOS)
bwhiteley / stats.swift
Created Feb 27, 2019
naive statistics playground for kid's homework.
View stats.swift
import Foundation
func mean(_ values: [Float]) -> Float {
return sum(values) / Float(values.count)
func median(_ values: [Float]) -> Float {
var sorted = values.sorted()
guard values.count > 0 else { return 0 }
while sorted.count > 2 {
bwhiteley /
Created Dec 12, 2017
Create GIF from screen recording (or other video)
# Requires ffmpeg and ImageMagick
View UIViewController with Xib
class DetailViewController: UIViewController {
@IBOutlet var label: UILabel! {
didSet {
label?.text = episode.title
let episode: Episode
init(episode: Episode) {

To start using Carthage with a new project:

  1. List your dependencies in a Cartfile. Refer to the Carthage documentation for details.
  2. Run carthage bootstrap --no-build --use-submodules
  3. Create a new workspace.
  4. Drag your existing App project into the workspace.
  5. Drag framework dependency projects from ./Carthage/Checkouts into the workspace.
  6. Go to the General tab of your target properties.
  7. Add framework dependencies to Linked Frameworks and Libraries. They will show up as Workspace frameworks.
  8. Add the same frameworks to Embedded Binaries.
  9. Note, the previous step will probably create duplicates in Linked Frameworks and Libraries. Delete the duplicates.
bwhiteley / Nil.swift
Last active Mar 2, 2017
NilLiteralConvertible can lead to surprising behavior
View Nil.swift
struct Foo : CustomStringConvertible {
let text:String
init(text:String) {
self.text = text
var description:String { return self.text }
bwhiteley / MyWKInterfaceController.swift
Last active Aug 29, 2015
Ensure UI updates to WatchKit Interface Controller only happen while the controller is active.
View MyWKInterfaceController.swift
"WatchKit ignores attempts to set values of interface objects
while your interface is inactive.... Modifications can be made
only during initialization of your interface controller and
between calls to willActivate and [didDeactivate]."
class MyWKInterfaceController: WKInterfaceController {
private var isActive:Bool = true
bwhiteley / gist:049e4bede49e71a6d2e2
Last active Oct 1, 2022
Initialize Swift subclass of UIView, designed in .xib
View gist:049e4bede49e71a6d2e2
// Create CustomView.xib, set File's Owner to CustomView.
// Link the top level view in the XIB to the contentView outlet.
class CustomView : UIView {
@IBOutlet private var contentView:UIView?
// other outlets
override init(frame: CGRect) { // for using CustomView in code
super.init(frame: frame)
bwhiteley /
Last active Dec 16, 2015
My WWDC ticket alert system. Run in screen on linux.
foo=$(curl $theSite)
echo $foo
oldFile=$(date +"%Y-%m-%d-%H_%M_%S").start
echo "$foo" > $oldFile
while true; do