Skip to content

Instantly share code, notes, and snippets.

View calebd's full-sized avatar

Caleb Davenport calebd

View GitHub Profile
@calebd
calebd / ActionSheetPresentationController.swift
Created July 18, 2017 02:42
Action Sheet Presentation Controller
// Created by Caleb Davenport on 7/14/17.
import UIKit
final class ActionSheetPresentationController: UIPresentationController {
// MARK: - Properties
private var dimmingView: UIView!
@calebd
calebd / Readme.markdown
Last active September 7, 2022 16:45
Run Loop Source

CFRunLoopSource is cool. It lets you build behavior similar to the mechanisms that drive setNeedsLayout and setNeedsDisplay in UIKit.

I found myself in need of something like this a couple of times. It's great to know that no matter how many times I say I need to update something, I will get a single callback at the end of the run loop that gives me a chance to perform my work.

Here is a little Swift wrapper that makes the API easier to deal with.

@calebd
calebd / AsynchronousOperation.swift
Last active September 21, 2024 15:28
Concurrent NSOperation in Swift
import Foundation
/// An abstract class that makes building simple asynchronous operations easy.
/// Subclasses must implement `execute()` to perform any work and call
/// `finish()` when they are done. All `NSOperation` work will be handled
/// automatically.
open class AsynchronousOperation: Operation {
// MARK: - Properties
import UIKit
class MyView: UIView {
// I could define this as UIPanGestureRecognizer! but that feels like the wrong solution
let panGestureRecognizer: UIPanGestureRecognizer
init(frame: CGRect) {
// Can't do this here because self is not initialized yet
@calebd
calebd / ArrayHelpers.swift
Last active November 4, 2022 15:17
Swift Helpers
extension Array {
func first() -> Element? {
if isEmpty {
return nil
}
return self[0]
}
func last() -> Element? {
@calebd
calebd / Readme.markdown
Last active August 29, 2015 14:00
NSData

Why does this work? In theory, creating a mutable version of an NSData object by referencing its original bytes using -[NSMutableData dataWithBytesNoCopy:length:freeWhenDone:], and then modifying those bytes would result in the original immutable object being modified as well.

It is worth noting that calling -[NSData dataWithBytesNoCopy:length:freeWhenDone:] results in both data objects having the same byte pointer address, and that memsetting the data returned by bytes on an immutable data object will in fact update the data that it represents.

Output:

DataTest[58994:303] original data pointer location: 0x10010a630
DataTest[58994:303] original data: <4c6f7265 6d206970 73756d20 646f6c6f 72207369 7420616d 65742c20 636f6e73 65637465 74757220 61646970 69736369 6e672065 6c69742e>
DataTest[58994:303] mutable data pointer location: 0x100201a00

I found an annoying behavior in UIColor today. If either saturation or brightness is 0 when creating an HSB color, other color values are ignored. This results in a valid color, and if drawn, it will be the correct color. But the values coming out of it are incorrect.

Assuming these defined values:

UIColor *color;
CGFloat inputHue, inputSaturation, inputBrightness, inputAlpha;
CGFloat outputHue, outputSaturation, outputBrightness, outputAlpha;

The URL http://pancake.apple.com/bags/hls?version=4.12 contains a plist, and inside that is coded another plist. Together, they make references to Apple IP addresses, ports, web services, Netflix, and Apple TV. What is this?

Edit: It looks like AVPlayer encounters an eror in the simulator that points to this URL as seen here: http://stackoverflow.com/questions/22569013/rtcreporting-pancake-apple-com-errors. Thanks to @mwhuss for sharing this with me.

Outer property list:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">

Keybase proof

I hereby claim:

  • I am calebd on github.
  • I am calebd (https://keybase.io/calebd) on keybase.
  • I have a public key whose fingerprint is 90E2 D6FA 4617 A20E F17B A0FD C9D4 36D2 93D8 1A54

To claim this, I am signing this object:

@calebd
calebd / Brewfile
Last active January 4, 2016 09:49
Brewfile
update
upgrade
install 'rbenv'
install 'ruby-build'
install 'redis'
install 'postgres'
install 'heroku'
install 'zsh-completions'
install 'pianobar'