Skip to content

Instantly share code, notes, and snippets.

@ole
ole / MeasurementParsing.swift
Last active January 27, 2017 18:04
Parse measurement expressions like `"5 m²"` and convert them into `Measurement` values. Uses the Objective-C runtime to find the known and valid symbols for a given unit (such as `UnitArea`). Adopts `ExpressibleByStringLiteral` for easy initialization.
// Parse measurement expressions like `"5 m²"` and convert them into `Measurement` values.
// Uses the Objective-C runtime to find the known and valid symbols for a given unit
// (such as `UnitArea`). Adopts `ExpressibleByStringLiteral` for easy initialization.
import ObjectiveC
enum ObjectiveCRuntime {
class Class {
let base: AnyClass
let str = "👨🏾‍🚒"
print(str.unicodeScalars.map { "0x\(String($0.value, radix: 16))" })
// → ["0x1f468", "0x1f3fe", "0x200d", "0x1f692"]
print(str.utf16.map { "0x\(String($0, radix: 16))" })
// → ["0xd83d", "0xdc68", "0xd83c", "0xdffe", "0x200d", "0xd83d", "0xde92"]
print(str.utf16.count)
// → 7
let utf16Offset = 2
@ole
ole / type-pattern-matching.swift
Last active October 12, 2017 16:51
Pattern matching for types. See https://twitter.com/Gernot/status/918490645118976000 for context.
func ~= <T, U> (pattern: T.Type, value: U.Type) -> Bool {
return pattern == value
}
func f<T>(_ type: T.Type) {
switch T.self {
case Int.self:
print("Int")
default:
print("Something else")
Process: Notes [17745]
Path: /Applications/Notes.app/Contents/MacOS/Notes
Identifier: com.apple.Notes
Version: 4.5 (863)
Build Info: Notes-863000000000000~1
Code Type: X86-64 (Native)
Parent Process: ??? [1]
Responsible: Notes [17745]
User ID: 501
let s = "Hello World"
let evenIndices = s.indices.enumerated()
.filter { $0.offset % 2 == 0 }
.map { $0.element }
for idx in evenIndices {
print(s[idx])
}
@ole
ole / geocode_coordinates.rb
Created January 23, 2014 18:23
Geocodes addresses on the command line and print the geo coordinates (latitude and longitude). See http://oleb.net/blog/2014/01/geocoding-automator-service/ for more info on how I am using this script.
#!/usr/bin/env ruby
# Determines the coordinates (latitude and longitude) of the places or
# addresses passed as arguments (either on the command line or via stdin)
# and prints the results.
# This script requires the Ruby Geocoder gem by Alex Reisner
# (http://www.rubygeocoder.com).
# To install the gem, run this command from a Terminal session:
#
@ole
ole / URLSession.swift
Created February 22, 2018 00:08
Paste this into a playground and let it run for 5 seconds. You’ll get a non-nil response *and* error.
import Foundation
import PlaygroundSupport
let bigFile = URL(string: "https://speed.hetzner.de/1GB.bin")!
let task = URLSession.shared.dataTask(with: bigFile) { (data, response, error) in
print("data: \(data)")
print("response: \(response)")
print("error: \(error)")
}
task.resume()
@ole
ole / adjustInfoPlist.sh
Last active April 23, 2018 14:53
A script for setting `UIFileSharingEnabled` and `LSSupportsOpeningDocumentsInPlace` in Xcode debug builds.
#!/bin/sh
if ( [ "$CONFIGURATION" == "Debug" ] ) then
echo "Enabling filesharing flags for '$CONFIGURATION' configuration in "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}""
/usr/libexec/PlistBuddy -c "Set :UIFileSharingEnabled true" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
/usr/libexec/PlistBuddy -c "Set :LSSupportsOpeningDocumentsInPlace true" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"
fi
@ole
ole / fun-with-string-interpolation.swift
Last active June 13, 2018 15:02
Fun with String Interpolation — For more information read my article at https://oleb.net/blog/2017/01/fun-with-string-interpolation/. — Dependencies: Foundation
/// An unescaped string from a potentially unsafe
/// source (such as user input)
struct UnsafeString {
var value: String
}
/// A string that either comes from a safe source
/// (e.g. a string literal in the source code)
/// or has been escaped.
struct SanitizedHTML {
import UIKit
let containingRect = CGRect(x: 0, y: 0, width: 100, height: 100)
let path1 = UIBezierPath(rect: containingRect)
let circleRect = CGRect(x: 50, y: 10, width: 80, height: 80)
let circleCenter = CGPoint(x: circleRect.midX, y: circleRect.midY)
let radius = circleRect.height / 2
/*