I have a category for the NSURLConnection and I use associated objects to extend the class to keep a reference to a custom class of mine so I can get later in other context when delegate and forward the call to the real target later. This way I can modify the desired data in certain points of runtime.
But for a reason when everything used to work properly, suddenly the objc_getAssociatedObject returns nil, meaning I don't get back my associated object and the whole process stops.
static void* KEY_CONNECTION_MYCLASS;
static MyClass* GetConnectionMyClass(NSURLConnection* connection)
static void AttachConnectionMyClass(NSURLConnection* connection, MyClass* myClass)
NSURLConnection* connection = (NSURLConnection*) self;
NSURLConnection* connection = (NSURLConnection*)self;
MyClass* myClass = GetConnectionMyClass(connection);
NSLog(@"MyClass is NOT NIL");
NSLog(@"MyClass is NIL");
You are using different keys when setting and getting the object.
& when setting, but not when getting. You should use in both cases.
A good practice is to define the key like so:
static void* KEY_CONNECTION_MYCLASS = &KEY_CONNECTION_MYCLASS;
This way, your typo would not have mattered. But it's good to fix typos.