Written by
Der Teilweise
on
on
Class object as delegate
This article about a lightweight HTTP request class is quite interesting. However, what never came to my mind (but seems completely consistent once you think about it) is in the second part of the article. Take a look at this code. The points of interest are:
- (void) downloadRSSFeed {
[…]
[SMWebRequest requestWithURL:url
delegate:(id)[self class]
context:nil];
[…]
}
+ (id) webRequest:(SMWebRequest *)webRequest
resultObjectForData:(NSData *)data
context:(id)context {
[…]
}
You can use the class object as delegate! This results in class methods being called instead of instance methods. Of course this is not always useful but in some cases this is great! The example in the linked article is one of the situations that benefit heavily: A combination of not retaining the object, calling the delegate in a thread and releasing the main object. It’s not as abstract as it sounds when you read the article. Maybe you have to read it twice.