Skip to content

Instantly share code, notes, and snippets.

@mattjgalloway
mattjgalloway / wtf_guard.swift
Last active September 26, 2016 06:32
WTF Swift
/**
* I have a URL and a UUID stored in UserDefaults, both as String.
* I want to extract both and convert the UUID string into an actual UUID.
* Here is something strange...
*/
// This works on iOS. But on watchOS, the guard returns, even though the URL and UUID exist in UserDefaults
guard
let accountURL = UserDefaults.standard.string(forKey: UserDefaultKeys.accountURL.rawValue),
let accountUUIDString = UserDefaults.standard.string(forKey: UserDefaultKeys.accountUUID.rawValue),
@mattjgalloway
mattjgalloway / gist:54577d4039a81a4b4168
Last active August 29, 2015 14:13
Running a block^WNSNull. Wait what?
// On iOS...
// This doesn't crash:
id null = @[[NSNull null]];
dispatch_block_t block = (dispatch_block_t)null[0];
block();
// This does:
id null = [NSNull null];
dispatch_block_t block = (dispatch_block_t)null;
@mattjgalloway
mattjgalloway / gist:e373c9af05070154a473
Created November 28, 2014 10:36
Objects that are isEqual: should have the same hash
(lldb) p (int) [0x7d35ec40 hash]
(int) $14 = 16406
(lldb) p (int) [0x79bef090 hash]
(int) $15 = 22
(lldb) p (BOOL) [0x79bef090 isEqual:0x7d35ec40]
(BOOL) $16 = YES
(lldb) p (BOOL) [0x7d35ec40 isEqual:0x79bef090]
@mattjgalloway
mattjgalloway / gist:6713937
Created September 26, 2013 13:10
Always remember what a BOOL is
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSUInteger, MyEnum) {
MyEnumA = 1 << 0,
MyEnumB = 1 << 1,
MyEnumC = 1 << 2,
MyEnumD = 1 << 3,
MyEnumE = 1 << 4,
MyEnumF = 1 << 5,
MyEnumG = 1 << 6,
@mattjgalloway
mattjgalloway / gist:6279363
Created August 20, 2013 09:36
Check if something responds to a certain selector, searching up until a certain class
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
@interface NSObject (MJGResponds)
+ (BOOL)mjg_hasImplementationForSelector:(SEL)selector searchUntilClass:(Class)cls;
@end
@implementation NSObject (MJGResponds)
+ (BOOL)mjg_hasImplementationForSelector:(SEL)selector searchUntilClass:(Class)cls {
@mattjgalloway
mattjgalloway / gist:3802388
Created September 28, 2012 22:27
Leaks detection not working for UIViewController subclass
@interface SubclassNSObject : NSObject
@property (nonatomic, strong) SubclassNSObject *other;
@end
@implementation SubclassNSObject
@end
@interface SubclassUIViewController : UIViewController
@property (nonatomic, strong) SubclassUIViewController *other;
@end