{"id":4418,"date":"2014-03-30T10:49:18","date_gmt":"2014-03-30T10:49:18","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/objective-c-weak-reference-to-super-class-collection-of-common-programming-errors\/"},"modified":"2014-03-30T10:49:18","modified_gmt":"2014-03-30T10:49:18","slug":"objective-c-weak-reference-to-super-class-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/objective-c-weak-reference-to-super-class-collection-of-common-programming-errors\/","title":{"rendered":"Objective C &#8211; Weak reference to super class?-Collection of common programming errors"},"content":{"rendered":"<p>You could define a helper method<\/p>\n<pre><code>-(void) helperMethod\n{\n    [super doStuff];\n    \/\/ ...\n    [super doOtherStuff];\n    \/\/ ...\n}\n<\/code><\/pre>\n<p>and then do<\/p>\n<pre><code>__weak MyClass *weakSelf = self;\n[self somethingWithCompletion:^(){\n    MyClass *strongSelf = weakSelf;\n   [strongSelf helperMethod];\n}];\n<\/code><\/pre>\n<p>A direct solution using the runtime methods looks like this:<\/p>\n<pre><code>__weak MyClass *weakSelf = self;\n[self somethingWithCompletion:^(){\n    MyClass *strongSelf = weakSelf;\n    if (strongSelf) {\n        struct objc_super super_data = { strongSelf, [MyClass superclass] };\n        objc_msgSendSuper(&amp;super_data, @selector(doStuff));\n    }\n});\n<\/code><\/pre>\n<p>Disadvantages (in my opinion):<\/p>\n<ul>\n<li>More (complicated) code.<\/li>\n<li>According to the &#8220;Objective-C Runtime Programming Guide&#8221;, you should never call the messaging functions directly in your code.<\/li>\n<li>Depending on the return type of the method, you would have to use <code>objc_msgSendSuper<\/code> or <code>objc_msgSendSuper_stret<\/code>.<\/li>\n<li>For methods taking arguments, you have to cast <code>objc_msgSendSuper<\/code> to the proper function type (thanks to @newacct).<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>You could define a helper method -(void) helperMethod { [super doStuff]; \/\/ &#8230; [super doOtherStuff]; \/\/ &#8230; } and then do __weak MyClass *weakSelf = self; [self somethingWithCompletion:^(){ MyClass *strongSelf = weakSelf; [strongSelf helperMethod]; }]; A direct solution using the runtime methods looks like this: __weak MyClass *weakSelf = self; [self somethingWithCompletion:^(){ MyClass *strongSelf = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4418","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4418","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4418"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4418\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}