Skip to content

Instantly share code, notes, and snippets.

@khanlou
khanlou / URLComponents+PathExtension.swift
Created July 13, 2017 18:17
Mutable pathExtension on URLComponents
// Swift 3
extension URLComponents {
var pathExtension: String {
get {
return NSString(string: self.path).pathExtension
}
set {
let nsStringPath = NSString(string: self.path)
let nsStringPathWithoutExtension = NSString(string: nsStringPath.deletingPathExtension)
extension Sequence {
func destructure() -> (Element, AnySequence<Element>)? {
var iterator = self.makeIterator()
guard let first = iterator.next() else { return nil }
return (first, AnySequence(IteratorSequence(iterator)))
}
}
import Foundation
protocol OptionalType {
associatedtype Wrapped
var value: Wrapped? { get }
}
extension Optional: OptionalType {
var value: Wrapped? {
@khanlou
khanlou / index.xml
Created August 11, 2017 16:09
Jekyll feed
---
layout: null
---
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>{{ site.title | xml_escape }}</title>
<description>{{ site.description | xml_escape }}</description>
<link>{{ site.real_url }}/</link>
<atom:link href="{{ "/feed.xml" | prepend: site.real_url }}" rel="self" type="application/rss+xml"/>
public struct Credentials {
public init(key: String, secret: String) {
self.key = key
self.secret = secret
}
public let key: String
public let secret: String
}
- (void)hideVolumeBezel {
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:CGRectZero];
volumeView.clipsToBounds = YES;
[self.view addSubview:volumeView];
}
- (void)beginDetectingVolumeClicks {
AVAudioSession* audioSession = [AVAudioSession sharedInstance];
[audioSession setActive:YES error:nil];
[audioSession setCategory:AVAudioSessionCategoryAmbient error:nil];
import Foundation
struct Person: Codable {
let name: String
}
let person = Person(name: "Soroush")
let archiver = NSKeyedArchiver(forWritingWith: NSMutableData())
{% for type in types.implementing.AutoRelationshipable %}
extension {{ type.name }} {
{% for variable in type.storedVariables %}
{% if variable.name|hasSuffix:"ID" and variable.annotations.relationshipType %}
var {% if variable.annotations.relationshipName %}{{ variable.annotations.relationshipName }}{% else %}{{ variable.name|replace:"ID","" }}{% endif %}: {{ variable.annotations.relationshipType }}? {
{% if variable.isOptional %}
guard let {{ variable.name }} = {{ variable.name }} else { return nil }
return FlatCache.shared.get(id: {{ variable.name }})
@khanlou
khanlou / Collection+Chunking.swift
Last active December 23, 2020 02:00
Collection chunking via @timvermeulen
extension Collection {
func chunk(size: Int) -> ChunkedCollection<Self> {
ChunkedCollection(self, size: size)
}
}
struct ChunkedCollection<Base: Collection>: Collection {
private let base: Base
import Vapor
import HTTP
import Foundation
extension Node {
var backDoorToRealValues: Any {
return self.wrapped.backDoorToRealValues
}
}