Skip to content

Instantly share code, notes, and snippets.

@brocoo
brocoo / Available UIFonts
Last active August 29, 2015 13:57
Available UIFonts on iOS
NSArray *fontFamilies = [UIFont familyNames];
for (int i = 0; i < [fontFamilies count]; i++){
NSString *fontFamily = fontFamilies[i];
NSArray *fontNames = [UIFont fontNamesForFamilyName:fontFamilies[i]];
NSLog (@"%@: %@", fontFamily, fontNames);
}
@brocoo
brocoo / Maths macros
Last active August 29, 2015 13:57
Maths macros
#define FLT_EQUAL(a, b) (fabs((a) - (b)) < FLT_EPSILON)
#define FLT_ZERO(a) (fabs(a) < FLT_EPSILON)
@brocoo
brocoo / Prevent iCloud Backup
Created April 23, 2014 14:00
This method should be implemented in a NSURL category
- (BOOL)addSkipBackupAttribute{
if (![[NSFileManager defaultManager] fileExistsAtPath:[self path]]) return NO;
NSError *error = nil;
BOOL success = [self setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error];
if(!success){
NSLog(@"Error excluding %@ from backup %@", [self lastPathComponent], error);
}
return success;
}
@brocoo
brocoo / Result.swift
Last active August 29, 2015 14:16
A result enum, storing a boxed type or a NSError meant to be return from any process (API call, etc) (Swift 1.2)
// MARK: - Box
final public class Box<T> {
public let unbox:T
public init(_ value: T) { self.unbox = value }
}
// MARK: - Result enum
public enum Result<T: Printable> {
@brocoo
brocoo / WebView.swift
Last active August 29, 2015 14:23
Swift 1.2 function to flush shared cookies
public func flushCookies() {
let storage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
if let cookieJar = storage.cookies as? [NSHTTPCookie] {
for cookie in cookieJar { storage.deleteCookie(cookie) }
}
NSUserDefaults.standardUserDefaults().synchronize()
}
@brocoo
brocoo / Flip.swift
Last active August 29, 2015 14:25
Flip UIView around any axis
extension CGFloat {
var toRadians: CGFloat {
return self * CGFloat(M_PI) / 180.0
}
var toDegress: CGFloat {
return self * 180 / CGFloat(M_PI)
}
}
@brocoo
brocoo / Settings.swift
Created July 22, 2015 20:24
Settings struct
struct Settings {
// Make sure to add the "-D DEBUG" flag in the project settings for the Swift Compiler
static var Debug: Bool {
#if DEBUG
return true
#else
return false
#endif
}
@brocoo
brocoo / Point.swift
Created July 22, 2015 22:19
CGPoint extension for SpriteKit
extension CGPoint {
public enum CoordinateSystem {
case UIKit
case SpriteKit
}
public func coordinates(from from: CoordinateSystem, to: CoordinateSystem) -> CGPoint {
if from == to { return self }
else {
@brocoo
brocoo / Extensions.swift
Created July 23, 2015 11:34
Perform closure once for a given key
// MARK: - NSUserDefaults
extension NSUserDefaults {
class func performOnceForKey(key: String, perform: () -> Void, elsePerform: (() -> Void)? = nil) {
let once = self.standardUserDefaults().objectForKey(key)
self.standardUserDefaults().setBool(true, forKey: key)
self.standardUserDefaults().synchronize()
if once == nil { perform() }
@brocoo
brocoo / CollectionType.swift
Created August 19, 2015 15:41
Add a firstMatching function for CollectionType
extension CollectionType {
/// Returns the first element where `predicate` returns `true` for the
/// corresponding value, or `nil` if such value is not found.
///
/// - Complexity: O(`self.count`).
func firstMatching(@noescape predicate: (Self.Generator.Element) -> Bool) -> Self.Generator.Element? {
for (_, element) in self.enumerate() {
if predicate(element) { return element }
}