Skip to content

Instantly share code, notes, and snippets.

@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> {
@alemar11
alemar11 / SwipeRefreshLayoutToggleScrollListener
Created March 31, 2016 10:30 — forked from NikolaDespotoski/SwipeRefreshLayoutToggleScrollListener
Fix for known bug when scrolling a RecyclerView inside SwipeRefreshLayout to the top and refresh is called prematurely, before the first item is shown.
public class SwipeRefreshLayoutToggleScrollListener extends RecyclerView.OnScrollListener {
private List<RecyclerView.OnScrollListener> mScrollListeners = new ArrayList<RecyclerView.OnScrollListener>();
private int mExpectedVisiblePosition = 0;
private SwipeRefreshLayout mSwipeLayout;
public SwipeRefreshLayoutToggleScrollListener(SwipeRefreshLayout swipeLayout) {
mSwipeLayout = swipeLayout;
}
public void addScrollListener(RecyclerView.OnScrollListener listener){
mScrollListeners.add(listener);
extension UIView {
func forEachSubviewOfType<V: UIView>(type: V.Type, @noescape apply block: V -> Void) {
for view in subviews {
if let view = view as? V {
block(view)
} else {
view.forEachSubviewOfType(V.self, apply: block)
}
}
}