Skip to content

Instantly share code, notes, and snippets.

@mhuusko5
mhuusko5 / KVODeadlock.m
Created Dec 6, 2016
KVO deadlock (seemingly internal global mutex being held, and attempting to swap with lock of object being observed or mutated)
View KVODeadlock.m
@interface NSObject ()
- (void)_addObserver:(id)arg1 forProperty:(id)arg2 options:(unsigned int)arg3 context:(void*)arg4;
- (void)_changeValueForKey:(id)arg1 key:(id)arg2 key:(id)arg3 usingBlock:(id /* block */)arg4;
- (void)_changeValueForKey:(id)arg1 usingBlock:(id /* block */)arg2;
- (void)_changeValueForKeys:(id*)arg1 count:(unsigned int)arg2 maybeOldValuesDict:(id)arg3 usingBlock:(id /* block */)arg4;
- (void)_didChangeValuesForKeys:(id)arg1;
- (void)_notifyObserversForKeyPath:(id)arg1 change:(id)arg2;
- (void)_notifyObserversOfChangeFromValuesForKeys:(id)arg1 toValuesForKeys:(id)arg2;
- (void)_willChangeValuesForKeys:(id)arg1;
@end
@mhuusko5
mhuusko5 / KVOCrash.m
Created Dec 5, 2016
KVO with multi-key path where observer is added/removed while non-last-key property is mutated, crashes
View KVOCrash.m
/* 2016-12-05 19:29:16.988026 Compass[71668:644974] [General] An uncaught exception was raised
2016-12-05 19:29:16.988060 Compass[71668:644974] [General] Cannot update for observer <TestObserver 0x600000001060> for the key path "nestedObject.nestedField" from <TestObject 0x600000026ee0>, most likely because the value for the key "nestedObject" has changed without an appropriate KVO notification being sent. Check the KVO-compliance of the TestObject class.
2016-12-05 19:29:16.988395 Compass[71668:644974] [General] (
0 CoreFoundation 0x00007fffaa71ee7b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00007fffbf303cad objc_exception_throw + 48
2 CoreFoundation 0x00007fffaa79d99d +[NSException raise:format:] + 205
3 Foundation 0x00007fffac0f5a62 -[NSKeyValueNestedProperty object:withObservance:didChangeValueForKeyOrKeys:recurse:forwardingValues:] + 830
4 Foundation 0x00007fffac0c8e88 NSKe
@mhuusko5
mhuusko5 / toRACSignal.swift
Created Nov 17, 2015
toRACSignal() for SignalProducer of any type, for Obj-C shimming a project with a Swift ReactiveCocoa based API
View toRACSignal.swift
public extension SignalProducerType {
func toRACSignal() -> RACSignal {
let signal = RACSignal.createSignal { subscriber in
let disposable = self.start {
switch $0 {
case let .Next(value):
guard let value = deepUnwrap(value) else {
subscriber.sendNext(nil)
break
}
@mhuusko5
mhuusko5 / iflet.m
Created Nov 12, 2015 — forked from CraigSiemens/iflet.m
if-let and guard macros for Objective C
View iflet.m
#import <Foundation/Foundation.h>
// VARIABLE must be a variable declaration (NSString *foo)
// VALUE is what you are checking is not nil
// WHERE is an additional BOOL condition
#define iflet(VARIABLE, VALUE) \
ifletwhere(VARIABLE, VALUE, YES)
#define ifletwhere(VARIABLE, VALUE, WHERE) \
@mhuusko5
mhuusko5 / Swift – func associatedValueForEnum
Last active Nov 12, 2015
Swift – func associatedValueForEnum(enumm: Any, atIndex index: Int) -> Any?
View Swift – func associatedValueForEnum
func caseLabelForEnum(enumm: Any) -> String {
let mirror = Mirror(reflecting: enumm)
return mirror.children.first?.label ?? String(enumm)
}
func associatedValueForEnum(enumm: Any) -> Any? {
let mirror = Mirror(reflecting: enumm)
return mirror.children.first?.value
@mhuusko5
mhuusko5 / Swift – func ensureType<T>
Created Oct 19, 2015
Swift – func ensureType<T>(type type: T.Type = T.self, transform: (Any) -> T)(_ any: Any) -> T
View Swift – func ensureType<T>
func ensureType<T>(type type: T.Type = T.self, transform: (Any) -> T)(_ any: Any) -> T {
return ensureType(any, type: type, transform: transform)
}
func ensureType<T>(any: Any, type: T.Type = T.self, transform: (Any) -> T) -> T {
if let any = any as? T {
return any
} else {
return transform(any)
}
@mhuusko5
mhuusko5 / Swift – func deepDescription
Last active Jan 21, 2021
Swift – func deepDescription(any: Any) -> String (pretty print any object, recursively)
View Swift – func deepDescription
func deepDescription(any: Any) -> String {
guard let any = deepUnwrap(any) else {
return "nil"
}
if any is Void {
return "Void"
}
if let int = any as? Int {
@mhuusko5
mhuusko5 / Swift – func deepUnwrap
Created Oct 19, 2015
Swift – func deepUnwrap(any: Any) -> Any?
View Swift – func deepUnwrap
func deepUnwrap(any: Any) -> Any? {
let mirror = Mirror(reflecting: any)
if mirror.displayStyle != .Optional {
return any
}
if let child = mirror.children.first where child.label == "Some" {
return deepUnwrap(child.value)
}
@mhuusko5
mhuusko5 / Swift – func asDictionary<K, V>
Created Oct 19, 2015
Swift – func asDictionary<K, V>(any: Any, type: (key: K.Type, value: V.Type) = (K.self, V.self)) -> [K: V]?
View Swift – func asDictionary<K, V>
func asDictionary<K, V>(any: Any, type: (key: K.Type, value: V.Type) = (K.self, V.self)) -> [K: V]? {
let mirror = Mirror(reflecting: any)
let properties = mirror.children
guard let displayStyle = mirror.displayStyle where displayStyle == .Dictionary else {
return nil
}
var dictionary = [K: V]()
@mhuusko5
mhuusko5 / Swift – func asArray<T>
Created Oct 19, 2015
Swift – func asArray<T>(any: Any, type: T.Type = T.self) -> [T]?
View Swift – func asArray<T>
func asArray<T>(any: Any, type: T.Type = T.self) -> [T]? {
let mirror = Mirror(reflecting: any)
let properties = Array(mirror.children)
guard let displayStyle = mirror.displayStyle where (displayStyle == .Collection || displayStyle == .Set || displayStyle == .Tuple) else {
return nil
}
var array = [T]()