Skip to content

Instantly share code, notes, and snippets.

@lipka
lipka / CGRectAspectFit.m
Created November 9, 2017 22:08 — forked from lanephillips/CGRectAspectFit.m
Objective-C code to fit a CGRect inside or outside another CGRect while maintaining aspect ratio. The fitted rectangle is centered on the target rectangle.
CGFloat ScaleToAspectFitRectInRect(CGRect rfit, CGRect rtarget)
{
// first try to match width
CGFloat s = CGRectGetWidth(rtarget) / CGRectGetWidth(rfit);
// if we scale the height to make the widths equal, does it still fit?
if (CGRectGetHeight(rfit) * s <= CGRectGetHeight(rtarget)) {
return s;
}
// no, match height instead
return CGRectGetHeight(rtarget) / CGRectGetHeight(rfit);
@lipka
lipka / Buildkite + Fastlane CI
Last active November 7, 2018 15:41
Tink's Buildkite and Fastlane setup for CI/CD
1. Install Xcode (available in the Mac App Store)
2. Install [Brew](https://brew.sh)
3. Install [Cocoapods](https://cocoapods.org)
4. Install [Fastlane](https://docs.fastlane.tools)
5. In Xcode, make sure that under Preferences/Locations the Command Line Tools field is not empty
6. Add provisoning profiles and certificates
7. Install [Buildkite agent](https://buildkite.com/docs/agent/v3/osx)
* `brew tap buildkite/buildkite`
* `brew install --token='INSERT-YOUR-AGENT-TOKEN-HERE' buildkite-agent`
* Run `brew info buildkite-agent` and execute the command displayed at the end to autostart the agent
@lipka
lipka / SafeTask.swift
Last active August 13, 2023 15:10
Force error handling in for Swift's Task when using structured concurrency.
// Swift's `Task` silently discards errors from throwable callsites. `SafeTask` forces you to explicitly handle errors at the callsite and helps you avoid mistakes with silently discarded errors.
@discardableResult func SafeTask<Success>(priority: TaskPriority? = nil, operation: @escaping () async -> Success) -> Task<Success, Never> where Success: Sendable {
return Task(priority: priority, operation: {
await operation()
})
}
@lipka
lipka / Cache.swift
Last active November 26, 2023 00:50
A first-class type-safe Swift cache based on NSCache with support for value types as keys.
import Foundation
// Based on https://www.swiftbysundell.com/articles/caching-in-swift/
final class Cache<Key: Hashable, Value> {
private let wrapped = NSCache<WrappedKey, Entry>()
func insert(_ value: Value, forKey key: Key) {
let entry = Entry(value: value)
wrapped.setObject(entry, forKey: WrappedKey(key))
@lipka
lipka / Throttle.swift
Last active September 1, 2023 21:44
Limits the rate at which a closure is executed by preventing it's execution until a specified amount of time has elapsed.
import Foundation
/**
Limits the rate at which a closure is executed by preventing it's execution until a specified amount of time has elapsed.
Example:
```
let fetchThrottle = Throttle(interval: 60)
fetchThrottle {