Skip to content

Instantly share code, notes, and snippets.

View radex's full-sized avatar
🚀
Full reusability

Radek Pietruszewski radex

🚀
Full reusability
View GitHub Profile
@radex
radex / lexer.swift
Last active February 15, 2017 13:08
Wrote a little lexer/tokenizer for fun. (Warning: I have no idea what I'm doing)
import Foundation
struct Stream {
let string: NSString
var position: Int
var matchingRange: NSRange {
return NSRange(location: position, length: string.length - position)
}
}
@radex
radex / optional-assignment.swift
Last active February 29, 2016 13:29
Optional assignment operator implementation. `foo ??= bar` will evaluate and assign bar to foo if and only if foo is nil. Equivalent to `||=` in Ruby and other languages.
infix operator ??= {
associativity right
precedence 90
assignment
}
func ??= <T>(inout variable: T?, expr: @autoclosure () -> T) {
if variable == nil {
variable = expr()
}
@radex
radex / keys.swift
Created October 28, 2014 16:10
Nested struct with static constants inside of a class is a neat way of managing magic string constants, like identifiers, user defaults or other dictionary keys, etc…
class Foo {
struct Keys {
static let keychainGroup = "com.foo.keychain-group"
static let enabled = "foo.enabled"
}
// now you can just write `Keys.enabled`, `Keys.keychainGroup` inside of the Foo class.
}
@radex
radex / Podfile
Created December 12, 2014 14:57
This is how you make a Podfile for a project with multiple platforms _and_ multiple targets. Easy, right?
source 'https://github.com/CocoaPods/Specs.git'
target :iOS do
platform :ios, '7.1'
link_with 'YourAppTarget', 'WidgetExtension', 'ShareExtension'
# pods...
end
target :Mac do
import Foundation
func foo() -> AnyObject? {
return NSDictionary()
}
func cast<A>(type: A.Type)(object: Any) -> A? {
return object as? A
}
import Foundation
func foo() -> AnyObject? {
return NSDictionary()
}
func cast<A>(type: A.Type)(object: Any) -> A? {
return object as? A
}
struct R {
struct Images {
static var doge: NSImage {
return NSImage(contentsOfFile: "...")!
}
}
}
R.Images.doge
@radex
radex / disable-webview-scroll.m
Created January 28, 2015 16:37
Disables UIWebView's behavior to shift web view on text input focus by intercepting and reversing all scrolling attempts
- (void)disableKeyboardScroll
{
self.webView.scrollView.scrollEnabled = NO;
self.webView.scrollView.delegate = self;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
self.webView.scrollView.bounds = self.webView.bounds;
}
@radex
radex / store-categories.txt
Created February 10, 2015 10:14
App Store and Play Store categories
Play Store
Books & Reference
Business
Comics
Communication
Education
Entertainment
Finance
Health & Fitness
let data = error.userInfo![AFNetworkingOperationFailingURLResponseDataErrorKey] as NSData
consoleLog(NSString(data: data, encoding: NSUTF8StringEncoding))