{"id":17935,"date":"2017-05-08T14:23:35","date_gmt":"2017-05-08T18:23:35","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=17935"},"modified":"2017-05-15T14:08:18","modified_gmt":"2017-05-15T18:08:18","slug":"foreach-using-objective-c-generics","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/05\/08\/foreach-using-objective-c-generics\/","title":{"rendered":"foreach Using Objective-C Generics"},"content":{"rendered":"<p><a href=\"https:\/\/twitter.com\/steipete\/status\/861558176008851457\">Peter Steinberger<\/a> shares an Objective-C macro that lets you write <code>foreach (object, collection)<\/code>, where <code>object<\/code> gets the proper type based on the collection&rsquo;s type parameter. The benefits: you save space and typing vs. a standard <code>for<\/code> loop, yet you still get static checking and auto-completion. The code ends up looking like my macro from the <a href=\"https:\/\/mjtsai.com\/blog\/2006\/07\/15\/cocoa-foreach-macro\/\">old<\/a> <a href=\"https:\/\/mjtsai.com\/blog\/2003\/12\/08\/cocoa_enumeration\/\">days<\/a> before <code>NSFastEnumeration<\/code> and <code>for&#8230;in<\/code> loops.<\/p>\n<p>Update (2017-05-15): <a href=\"https:\/\/twitter.com\/steipete\/status\/864074005715849216\">Peter Steinberger<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/steipete\/status\/864074005715849216\">\n<p>I wrote a header that overrides mutable\/Copy to pass along types+generic type data in Objective-C.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Peter Steinberger shares an Objective-C macro that lets you write foreach (object, collection), where object gets the proper type based on the collection&rsquo;s type parameter. The benefits: you save space and typing vs. a standard for loop, yet you still get static checking and auto-completion. The code ends up looking like my macro from the [&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":[69,31,1380,30,1381,941,54,71],"class_list":["post-17935","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-ios","tag-ios-10","tag-mac","tag-macos-10-12","tag-macros","tag-objective-c","tag-programming"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17935","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=17935"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17935\/revisions"}],"predecessor-version":[{"id":17984,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17935\/revisions\/17984"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=17935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=17935"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=17935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}