{"id":9917,"date":"2014-10-19T13:33:31","date_gmt":"2014-10-19T17:33:31","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=9917"},"modified":"2014-10-19T14:24:55","modified_gmt":"2014-10-19T18:24:55","slug":"mutable-collections-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2014\/10\/19\/mutable-collections-in-swift\/","title":{"rendered":"Mutable Collections in Swift"},"content":{"rendered":"<p><a href=\"https:\/\/twitter.com\/bffmike\/status\/523244788691107841\">Mike R. Manzano<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/bffmike\/status\/523244788691107841\"><p>How do you create an var that holds an immutable Array? As in a var that you can assign different immutable Arrays to?<\/p><\/blockquote>\n<p><a href=\"http:\/\/bjhomer.blogspot.com\/2014\/10\/mutable-collections-in-swift.html\">BJ Homer<\/a>:<\/p>\n<blockquote cite=\"http:\/\/bjhomer.blogspot.com\/2014\/10\/mutable-collections-in-swift.html\">\n<p>Because Swift arrays and dictionaries can never be shared, there is no distinction between mutating an existing collection and re-assigning a new collection. The behavior of the code is exactly the same. In either case, the owner&rsquo;s setter method is called whenever the array is modified.<\/p>\n<p>So to answer the original question, there is no syntax to specify a variable that holds an immutable array because there is nothing that such syntax would add. Swift addresses the issues that made <code>NSArray<\/code> and <code>NSMutableArray<\/code> necessary in the first place. If you need a shared array, you can still use the Cocoa types. In every other case, Swift&rsquo;s solution is safer, simpler, and more concise.<\/p><\/blockquote>\n<p>On the whole, I think this is probably a good direction. The downsides would seem to be that the performance model is less clear and that it&rsquo;s more work to write your own data types as struct-class pairs.<\/p>\n<p>One somewhat common pattern in my Objective-C code is a (often recursive) method that takes a mutable array or dictionary as a parameter and builds it up. You can&rsquo;t do this with <code>var<\/code> in Swift because that only lets you modify the collection within the method. However, you can use <code>inout<\/code> to have Swift &ldquo;return&rdquo; the last value to the caller.<\/p>\n<p>This is not the same as passing around an <code>NSMutableArray<\/code>, though. For example, consider what would happen if there were multiple threads involved. Also, <code>inout<\/code> only lasts for the duration of the method; the collection cannot (as far as I know) be stashed in another object and then mutated (back in the caller) later.<\/p>\n<p>Update (2014-10-19): <a href=\"https:\/\/twitter.com\/nuoji\/status\/523897038455726080\">Christoffer Lern&ouml;<\/a> responds via Twitter.<\/p>","protected":false},"excerpt":{"rendered":"<p>Mike R. Manzano: How do you create an var that holds an immutable Array? As in a var that you can assign different immutable Arrays to? BJ Homer: Because Swift arrays and dictionaries can never be shared, there is no distinction between mutating an existing collection and re-assigning a new collection. The behavior of 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,46,138,71,901],"class_list":["post-9917","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-languagedesign","tag-optimization","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9917","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=9917"}],"version-history":[{"count":4,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9917\/revisions"}],"predecessor-version":[{"id":9926,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9917\/revisions\/9926"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=9917"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=9917"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=9917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}