Skip to content

Instantly share code, notes, and snippets.

var inStr:String
var outStr:String
// bad IP-literal (there are no square brackets around the address)
inStr = "2606:2800:220:1:248:1893:25c8:1946"
outStr = inStr.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
// outStr = "2606%3A2800%3A220%3A1%3A248%3A1893%3A25c8%3A1946"
// valid IP-literal (there are square brackets around the address)
inStr = "[2606:2800:220:1:248:1893:25c8:1946]"
@monkeym4ster
monkeym4ster / docker.sh
Created July 6, 2017 06:04
Docker: save/load container using tgz file (tar.gz)
#for not running docker, use save:
docker save <dockernameortag> | gzip > mycontainer.tgz
#for running or paused docker, use export:
docker export <dockernameortag> | gzip > mycontainer.tgz
#load
gunzip -c mycontainer.tgz | docker load
@mbuchetics
mbuchetics / codeableEnum.swift
Created June 30, 2017 09:30 — forked from reckenrode/codeableEnum.swift
Implement Codable on an enum
struct User: Codable {
var name: String
var email: String
var id: String
var metadata: [String: MetadataType]
enum CodingKeys: String, CodingKey {
case name, email, id, metadata
}
}
@sjoerdvisscher
sjoerdvisscher / minimal.swift
Created June 28, 2017 14:42
Using Decodable to generate a minimal value
struct MinimalDecoder : Decoder {
var codingPath = [CodingKey?]()
var userInfo = [CodingUserInfoKey : Any]()
public func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> {
return KeyedDecodingContainer(MinimalKeyedDecodingContainer<Key>(decoder: self))
}
public func unkeyedContainer() throws -> UnkeyedDecodingContainer {
return DecodingContainer(decoder: self)
@mminer
mminer / MyService.swift
Last active April 23, 2024 23:00
Components of XPC service.
import Foundation
class MyService: NSObject, MyServiceProtocol {
func upperCaseString(_ string: String, withReply reply: @escaping (String) -> Void) {
let response = string.uppercased()
reply(response)
}
}
@takasek
takasek / CSVDecoder.swift
Last active September 7, 2018 23:12
Swift4のCodableに対応した、独自のDecoder(CSVDecoder)を実装してみよう ref: http://qiita.com/takasek/items/8bcb2f9169fbe2a0593e
import Foundation
//===----------------------------------------------------------------------===//
// CSV Decoder
//===----------------------------------------------------------------------===//
/// `CSVDecoder` facilitates the decoding of CSV into semantic `Decodable` types.
/// structでなくclassなのは、JSONDecoderやPlistDecoderの場合にはoptionを適宜切り替えつつdecodeしていけるようにだと思う
/// 実際の Decoder プロトコルへの適合は、fileprivateな _CSVRowDecoder 型を通して行う。
open class CSVDecoder {
@jerel
jerel / stream_csv.py
Created June 9, 2017 15:29
Easily export a massive dataset from Django to the browser as a streaming CSV file
import csv
from StringIO import StringIO
from django.http import StreamingHttpResponse
class StreamCSV(object):
def __init__(self):
self._buffer = StringIO()
self._writer = csv.writer(self._buffer)
@wattnpapa
wattnpapa / MKMapViewZoomLevel.swift
Created May 10, 2017 13:21
MKMapViewZoomLevel.swift
//
// MKMapViewZoomLevel.swift
//
// Created by Johannes Rudolph on 10.05.17.
// Based on http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/
//
import Foundation
import MapKit
@j-j-m
j-j-m / commonprofile.metal
Created May 8, 2017 00:20
Can't access GL Uniforms in Metal shader modifier? Apple docs for SCNShadable written in terms of GL? Pulling your hair out?... this will help.
////////////////////////////////////////////////
// CommonProfile Shader v2
#import <metal_stdlib>
using namespace metal;
#ifndef __SCNMetalDefines__
#define __SCNMetalDefines__
@KyNorthstar
KyNorthstar / URL function tests output
Last active September 21, 2018 22:46
Testing ambiguously-named and -documented URL functions
Control:
file:/// => file:///
file:///etc/ => file:///etc/
~/ -- file:/// => ~/ -- file:///
~/Desktop -- file:/// => ~/Desktop -- file:///
~/.bash_profile -- file:/// => ~/.bash_profile -- file:///
./ -- file:/// => ./ -- file:///
../ -- file:/// => ../ -- file:///
file:///etc/../ => file:///etc/../
~/../ -- file:/// => ~/../ -- file:///