Skip to content

Instantly share code, notes, and snippets.

// known-good: Swift 3
import UIKit
/// for a UIView that can be populated via `configure`
protocol ConfigurableView : class {
associatedtype Value
func configure(_ value:Value)
}
/// for a UICollectionViewCell wrapping a UIView
/**
Generic CollectionViewCell, which can be specialized to wrap a UIView.
To use this, you can simply define a typealias like so:
```
class FooView : UIVIew { /* ... */ }
typealias FooCell = WrappedCollectionViewCell<FooView>
```
@algal
algal / ScaleAspectFitImageView.swift
Last active September 24, 2023 10:19
UIImageView subclass that works with Auto Layout to express its desired aspect ratio
import UIKit
// known-good: Xcode 8.2.1
/**
UIImageView subclass which works with Auto Layout to try
to maintain the same aspect ratio as the image it displays.
This is unlike the usual behavior of UIImageView, where the
@algal
algal / URL+Data.swift
Created February 23, 2017 19:45
Extract the data from certain "data:" URIs
// known-good Xcode 8.2.1
extension URL
{
/**
Returns the data from a URI with the "data:" scheme, which includes the optional "base64" annotation, and which has a mediatype that is encoded in ASCII. As described in https://tools.ietf.org/html/rfc2397.
*/
var asData:Data?
{
guard
let scheme = self.scheme, scheme == "data",
import Foundation
// machine, version, release, nodename, sysname
extension utsname : CustomStringConvertible {
static var `default`:utsname {
var x = utsname()
uname(&x)
return x
}
@algal
algal / CircleView.swift
Last active October 10, 2024 04:53
Custom UIView hosting a CAShapeLayer
// known-good: Xcode 8.2.1, Swift 3, 2017-02-08
/**
Draws a circle, centered in the view, with the specified radius
*/
class CircleView: UIView
{
// API
var circleRadius:CGFloat = 15 {
didSet { self.setNeedsLayout() }
@algal
algal / Extra Logging for My Great App.mobileconfig
Created February 1, 2017 04:32 — forked from zwaldowski/Extra Logging for My Great App.mobileconfig
Apple Configuration Profile for Logging in iOS 10 and macOS Sierra
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<!-- iOS 10, macOS Sierra, and friends bring a new logging subsystem that's
supposed to scale from the kernel, up to frameworks, and up to apps. It defaults
to a more regimented, privacy-focused approach that large apps and complex
systems need.
It, along with Activity Tracing introduced in iOS 8 and macOS Yosemite and the
Console app in macOS Sierra, hope to help you graduate from caveman debugging to
@algal
algal / update-carthage-managed-dependencies.sh
Created December 6, 2016 16:36
carthage as a version-aware downloader
#!/bin/sh
command -v carthage >/dev/null 2>&1 || { echo >&2 "I require carthage but it's not installed. Aborting."; exit 1; }
carthage update --no-build
# A policy proposal:
#
# We use the "carthage" tool only to download the source code of
# third-party libraries that we depend on.
@algal
algal / String+replaceOccurence.swift
Created November 28, 2016 01:49
Mutating in-place replacement of a substring of a String
// known-good Swift3
extension String {
/// Performs in-place replacement of first occurence of a substring
mutating func replaceFirstOccurence(of query:String, with replacement:String) {
guard let firstRange = self.range(of: query)
else { return }
self.replaceSubrange(firstRange, with: replacement)
}
}
// Swift 3, macos/Linux
#if os(Linux)
import Glibc
#else
import Darwin
#endif
class RandomByteGenerator {
private let f = fopen("/dev/urandom","r")