{"id":11386,"date":"2015-06-01T11:13:24","date_gmt":"2015-06-01T15:13:24","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=11386"},"modified":"2015-06-01T11:13:24","modified_gmt":"2015-06-01T15:13:24","slug":"weakify-and-strongify-macros","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/06\/01\/weakify-and-strongify-macros\/","title":{"rendered":"@weakify and @strongify Macros"},"content":{"rendered":"<p><a href=\"http:\/\/holko.pl\/2015\/05\/31\/weakify-strongify\/\">Arkadiusz Holko<\/a>:<\/p>\n<blockquote cite=\"http:\/\/holko.pl\/2015\/05\/31\/weakify-strongify\/\"><p><a href=\"https:\/\/github.com\/jspahrsummers\/libextobjc#features\">Original implementation<\/a> of @weakify and @strongify macros is complex because they accept more than one parameter. To make the analysis simpler, we&rsquo;ll introduce our own versions, accepting only one parameter each:<\/p>\n<pre>#define weakify(var) __weak typeof(var) AHKWeak_##var = var;\n\n#define strongify(var) \\\n_Pragma(\"clang diagnostic push\") \\\n_Pragma(\"clang diagnostic ignored \\\"-Wshadow\\\"\") \\\n__strong typeof(var) var = AHKWeak_##var; \\\n_Pragma(\"clang diagnostic pop\")<\/pre>\n<p>[&#8230;]<\/p>\n<p>In the block, <code>self<\/code> is overshadowed by a local variable with the same name.\nThen, <code>self<\/code> can be used safely inside the block, because it references that local variable, which is held strongly, but lives only until the block ends executing.\nEven the code <strong>not written by us<\/strong> can use <code>self<\/code> safely, e.g. <a href=\"http:\/\/sealedabstract.com\/code\/nsnotificationcenter-with-blocks-considered-harmful\/\"><code>NSAssert<\/code> macro<\/a>.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Arkadiusz Holko: Original implementation of @weakify and @strongify macros is complex because they accept more than one parameter. To make the analysis simpler, we&rsquo;ll introduce our own versions, accepting only one parameter each: #define weakify(var) __weak typeof(var) AHKWeak_##var = var; #define strongify(var) \\ _Pragma(\"clang diagnostic push\") \\ _Pragma(\"clang diagnostic ignored \\\"-Wshadow\\\"\") \\ __strong typeof(var) var [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[168,941,571,54,71],"class_list":["post-11386","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-blocks","tag-macros","tag-memory-management","tag-objective-c","tag-programming"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11386","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=11386"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11386\/revisions"}],"predecessor-version":[{"id":11387,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11386\/revisions\/11387"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=11386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=11386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=11386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}