Skip to content

Instantly share code, notes, and snippets.

@proteye
proteye / rsa_pem.dart
Last active March 19, 2024 08:59
How to encode/decode RSA private/public keys to PEM format in Dart with asn1lib and pointycastle
import 'dart:convert';
import 'dart:math';
import 'dart:typed_data';
import "package:pointycastle/export.dart";
import "package:asn1lib/asn1lib.dart";
List<int> decodePEM(String pem) {
var startsWith = [
"-----BEGIN PUBLIC KEY-----",
"-----BEGIN PRIVATE KEY-----",
@nathanfjohnson
nathanfjohnson / String+HTML.swift
Last active November 23, 2021 15:09 — forked from mwaterfall/StringExtensionHTML.swift
Decoding HTML Entities in Swift 4
// Swift 4
// Check out the history for contributions and acknowledgements.
extension String {
/// Returns a new string made by replacing all HTML character entity references with the corresponding character.
///
/// - Returns: decoded string
func decodingHTMLEntities() -> String {
var result = String()
var position = startIndex
@lattner
lattner / async_swift_proposal.md
Last active April 21, 2024 09:43 — forked from oleganza/async_swift_proposal.md
Concrete proposal for async semantics in Swift

Async/Await for Swift

Introduction

Modern Cocoa development involves a lot of asynchronous programming using closures and completion handlers, but these APIs are hard to use. This gets particularly problematic when many asynchronous operations are used, error handling is required, or control flow between asynchronous calls gets complicated. This proposal describes a language extension to make this a lot more natural and less error prone.

This paper introduces a first class Coroutine model to Swift. Functions can opt into to being async, allowing the programmer to compose complex logic involving asynchronous operations, leaving the compiler in charge of producing the necessary closures and state machines to implement that logic.

@gbitaudeau
gbitaudeau / Int+Extenstion.swift
Created March 3, 2017 09:44
iOS solution to convert large numbers to smaller format. See : http://stackoverflow.com/a/35504720/1661338
extension Int {
func formatUsingAbbrevation () -> String {
let numFormatter = NSNumberFormatter()
typealias Abbrevation = (threshold:Double, divisor:Double, suffix:String)
let abbreviations:[Abbrevation] = [(0, 1, ""),
(1000.0, 1000.0, "K"),
(100_000.0, 1_000_000.0, "M"),
(100_000_000.0, 1_000_000_000.0, "B")]
import Foundation
extension String {
/// Converts an ISO 8601 formatted `String` into `NSDateComponents`.
///
/// - Note: Does not accept fractional input (e.g.: P3.5Y), must be integers (e.g.: P3Y6M).
/// - SeeAlso: https://en.wikipedia.org/wiki/ISO_8601#Durations
/// - Returns: If valid ISO 8601, an `NSDateComponents` representation, otherwise `nil`.
func ISO8601DateComponents() -> NSDateComponents? {
// Regex adapted from Moment.js https://github.com/moment/moment/blame/develop/src/lib/duration/create.js#L16
@alexisakers
alexisakers / Heroku-Vapor-Docker-Tutorial.md
Last active December 26, 2022 12:03
How to run Vapor with Docker in Heroku

How to run Vapor with Docker in Heroku

In this tutorial you'll learn how to run a Vapor server in a Docker container on Heroku.

Recently, Heroku added the ability to run Docker images on their Runtime. You can use this system instead of the traditional buildpack-based system for various reasons, mainly for having more control over the environment your server will run on.

Prerequisites

To complete this tutorial, you'll need:

@gonzalezreal
gonzalezreal / ios-cell-registration-swift.md
Last active March 13, 2024 15:18
iOS Cell Registration & Reusing with Swift Protocol Extensions and Generics

iOS Cell Registration & Reusing with Swift Protocol Extensions and Generics

A common task when developing iOS apps is to register custom cell subclasses for both UITableView and UICollectionView. Well, that is if you don’t use Storyboards, of course.

Both UITableView and UICollectionView offer a similar API to register custom cell classes:

public func registerClass(cellClass: AnyClass?, forCellWithReuseIdentifier identifier: String)
public func registerNib(nib: UINib?, forCellWithReuseIdentifier identifier: String)
import Foundation
extension Dictionary {
mutating public func setValue(val: AnyObject, forKeyPath keyPath: String) {
var keys = keyPath.componentsSeparatedByString(".")
guard let first = keys.first as? Key else { print("Unable to use string as key on type: \(Key.self)"); return }
keys.removeAtIndex(0)
if keys.isEmpty, let settable = val as? Value {
self[first] = settable
} else {
@jlalvarez18
jlalvarez18 / gist:123f9827a0e7f75a8e08
Last active August 29, 2015 14:24
Swift Dynamic UITableView Header View
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
self.layoutHeaderView(size.width)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.layoutHeaderView(self.tableView.bounds.width)
}
@andymatuschak
andymatuschak / CollectionViewDataSource.swift
Last active February 12, 2021 09:44
Type-safe value-oriented collection view data source
//
// CollectionViewDataSource.swift
// Khan Academy
//
// Created by Andy Matuschak on 10/14/14.
// Copyright (c) 2014 Khan Academy. All rights reserved.
//
import UIKit