Skip to content

Instantly share code, notes, and snippets.

@alemar11
alemar11 / CachedDateFormatter.swift
Created November 20, 2015 22:08 — forked from mluton/CachedDateFormatter.swift
Swift class that instantiates and caches NSDateFormatter objects
class CachedDateFormatter {
static let sharedInstance = CachedDateFormatter()
var cachedDateFormatters = [String: NSDateFormatter]()
func formatterWith(#format: String, timeZone: NSTimeZone = NSTimeZone.localTimeZone(), locale: NSLocale = NSLocale(localeIdentifier: "en_US")) -> NSDateFormatter {
let key = "\(format.hashValue)\(timeZone.hashValue)\(locale.hashValue)"
if let cachedDateFormatter = cachedDateFormatters[key] {
return cachedDateFormatter
}
// http://blog.krzyzanowskim.com
import Cocoa
struct ChunkSequence<Element>: SequenceType {
let chunkSize: Array<Element>.Index
let collection: Array<Element>
func generate() -> AnyGenerator<ArraySlice<Element>> {
var offset:Array<Element>.Index = collection.startIndex
@alemar11
alemar11 / SwiftStack.swift
Created January 8, 2016 10:09 — forked from ankitspd/SwiftStack.swift
A generic stack in swift
protocol StackType {
typealias Element
mutating func push(element: Element)
mutating func pop() -> Element?
}
final class BufferStorage<Element> {
private var ptr: UnsafeMutablePointer<Element>
private let capacity: Int
@alemar11
alemar11 / NSObject+PublicClass.swift
Created January 15, 2016 16:14
Determine if a random NSObject subclass instance is a public or private framework (does not test for your objects)
import Foundation
extension NSObject {
var isPublicClass: Bool {
return self.dynamicType.isPublicClass
}
class var isPublicClass: Bool {
return _PUBLIC_IOS_CLASSES.contains(NSStringFromClass(self))
}
//
// UILabel+JumpingDots.swift
// JumpingDots
//
// Copyright (c) 2016 Arkadiusz Holko. All rights reserved.
//
import UIKit
import ObjectiveC
@alemar11
alemar11 / [Localization]ForceFallbackLanguage.m
Created February 21, 2016 16:56 — forked from ariok/[Localization]ForceFallbackLanguage.m
Force the translation to a defined language. (I use this function to force the fallback language to Italian. I didn't find a way to make NSLocalizedString work with italian as fallback. your comments/suggestions are welcome :) )
NSString * L(NSString * translation_key, NSString * lang) {
NSString * s = NSLocalizedString(translation_key, nil);
// Force translation as "Lang" language if no translations are found
if ([s isEqualToString:translation_key]) {
NSString * path = [[NSBundle mainBundle] pathForResource:lang ofType:@"lproj"];
NSBundle * languageBundle = [NSBundle bundleWithPath:path];
s = [languageBundle localizedStringForKey:translation_key value:@"" table:nil];
}
@alemar11
alemar11 / script.swift
Created February 24, 2016 14:49 — forked from zats/script.swift
Update all your plugins for the latest Xcode beta with a single
#!/usr/bin/env xcrun swift
// $ chmod +x script.swift
// $ ./script.swift
// or $ ./script.swift -xcode=/Applications/Xcode-beta.app
import Foundation
@noreturn private func failWithError(message: String) {
print("🚫 \(message)")
@alemar11
alemar11 / TypeErasure.swift
Created March 3, 2016 08:20 — forked from gwengrid/TypeErasure.swift
Example of type erasure with Pokemon
class Thunder { }
class Fire { }
protocol Pokemon {
typealias PokemonType
func attack(move:PokemonType)
}
struct Pikachu: Pokemon {
typealias PokemonType = Thunder
import UIKit
struct Person {
let name: String
let city: String
}
let people = [
Person(name: "Chris", city: "Berlin"),
Person(name: "Natasha", city: "Tokyo"),
@alemar11
alemar11 / Queue.swift
Created March 15, 2016 08:50 — forked from zeeshankhan/Queue.swift
Queue using value type generics and memory pointers
protocol QueueType {
typealias Element
mutating func enqueue(element: Element)
mutating func dequeue() -> Element?
func peek() -> Element?
}
final class Storage<Element> {