{"id":13413,"date":"2016-02-03T13:57:24","date_gmt":"2016-02-03T18:57:24","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=13413"},"modified":"2016-02-03T13:57:24","modified_gmt":"2016-02-03T18:57:24","slug":"value-vs-reference-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/02\/03\/value-vs-reference-in-swift\/","title":{"rendered":"Value vs. Reference in Swift"},"content":{"rendered":"<p><a href=\"https:\/\/twitter.com\/owensd\/status\/694270364608647168\">David Owens II<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/owensd\/status\/694270364608647168\"><p>This is a general concern of mine in Swift: how to actually know what has value vs. reference semantics.<\/p><\/blockquote>\n<p>Swift adds <code>let<\/code> and <code>var<\/code>, which fixes one problem. But there is no way to see what is a <code>struct<\/code> or a <code>class<\/code>, which adds another. It&rsquo;s even more complicated in the context of the <a href=\"http:\/\/mjtsai.com\/blog\/2016\/01\/13\/better-translation-of-objective-c-apis-into-swift\/\">proposal<\/a> to remove the &ldquo;NS&rdquo; prefix from Foundation class names.<\/p>\n<p><a href=\"http:\/\/article.gmane.org\/gmane.comp.lang.swift.evolution\/5503\/match=nate+cook\">Nate Cook<\/a>:<\/p>\n<blockquote cite=\"http:\/\/article.gmane.org\/gmane.comp.lang.swift.evolution\/5503\/match=nate+cook\"><p>As an example, the seemingly similar <code>Set<\/code> and <code>CountedSet<\/code> types produce different results from nearly identical code. Swift&rsquo;s Set type has value semantics, so changes to a copy don&rsquo;t affect the original Set instance [&#8230;] <code>CountedSet<\/code> (n&eacute;e <code>NSCountedSet<\/code>), on the other hand, uses reference semantics, so changes to a copy of an instance. This is true whether the copy is an explicit one made via assignment or the implicit copy made when calling a function with <code>CountedSet<\/code> as a parameter. This example shows how nearly code nearly identical to the above produces very different results [&#8230;]<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>David Owens II: This is a general concern of mine in Swift: how to actually know what has value vs. reference semantics. Swift adds let and var, which fixes one problem. But there is no way to see what is a struct or a class, which adds another. It&rsquo;s even more complicated in the context [&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":[46,71,901],"class_list":["post-13413","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/13413","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=13413"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/13413\/revisions"}],"predecessor-version":[{"id":13414,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/13413\/revisions\/13414"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=13413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=13413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=13413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}