{"id":17991,"date":"2017-05-15T14:55:26","date_gmt":"2017-05-15T18:55:26","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=17991"},"modified":"2017-05-15T22:19:03","modified_gmt":"2017-05-16T02:19:03","slug":"searching-for-swift-objects-by-type","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/05\/15\/searching-for-swift-objects-by-type\/","title":{"rendered":"Searching for Swift Objects by Type"},"content":{"rendered":"<p><a href=\"http:\/\/timekl.com\/blog\/2017\/05\/14\/swift-tricks-searching-for-objects-by-type\/\">Tim Ekl<\/a>:<\/p>\n<blockquote cite=\"http:\/\/timekl.com\/blog\/2017\/05\/14\/swift-tricks-searching-for-objects-by-type\/\"><p>This rubbed me the wrong way, specifically because of the <code>is SpecialView<\/code>\/<code>as!\nSpecialView<\/code> combination. It seemed like I should be able to do the type check\n<em>once<\/em> and get the object back as that type, maybe using <code>as?<\/code>.<\/p><p>[&#8230;]<\/p><p>However, there&rsquo;s a cost here: where previously we&rsquo;d stop enumerating subviews\nonce we found the first SpecialView, with <code>flatMap<\/code> we run all the way through\nthe array before getting the first result. If <code>subviews<\/code> is an especially large\narray, this could become a performance hotspot very quickly.<\/p><\/blockquote>\n<p>In Objective-C, the obvious <code>for<\/code> loop or array-searching closure would do the right thing, without the need for concepts like <code>flatMap<\/code> and <code>lazy<\/code>. Then again, so would a Swift <code>for<\/code> loop with <code>if let<\/code>, but people would tend not to write it that way.<\/p>\n<p>If you wanted to generalize this sort of view search in Objective-C, you could pass a class as a parameter. That doesn&rsquo;t really work in Swift because you would end up with an awkward <code>Any?<\/code> return value. So you get into generics. You can pass a parameter of type <code>Any.Type<\/code>, but that doesn't really help because Swift can&rsquo;t use this to determine the return type. Instead, you could make the function take no parameters, passing the type as a generic parameter that&rsquo;s inferred based on the calling context. However, I think that makes the call sites look weird, and you still end up with an optional to unwrap.<\/p>","protected":false},"excerpt":{"rendered":"<p>Tim Ekl: This rubbed me the wrong way, specifically because of the is SpecialView\/as! SpecialView combination. It seemed like I should be able to do the type check once and get the object back as that type, maybe using as?.[&#8230;]However, there&rsquo;s a cost here: where previously we&rsquo;d stop enumerating subviews once we found the first [&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,30,54,71,901],"class_list":["post-17991","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-ios","tag-mac","tag-objective-c","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17991","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=17991"}],"version-history":[{"count":3,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17991\/revisions"}],"predecessor-version":[{"id":17998,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17991\/revisions\/17998"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=17991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=17991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=17991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}