{"id":12187,"date":"2015-09-04T10:13:01","date_gmt":"2015-09-04T14:13:01","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=12187"},"modified":"2015-09-04T10:15:59","modified_gmt":"2015-09-04T14:15:59","slug":"struct-semantics-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/09\/04\/struct-semantics-in-swift\/","title":{"rendered":"Struct Semantics in Swift"},"content":{"rendered":"<p><a href=\"http:\/\/chris.eidhof.nl\/posts\/struct-semantics-in-swift.html\">Chris Eidhof<\/a>:<\/p>\n<blockquote cite=\"http:\/\/chris.eidhof.nl\/posts\/struct-semantics-in-swift.html\">\n<p>Instead, we can change our <code>mutableData<\/code> accessor to prevent copying when there are no other references to the <code>data<\/code> variable. To do this, we need to check if the data is uniquely referenced. In other words, we only make a copy of the data if the data is shared. We can do this using the <code>isUniquelyReferencedNonObjC<\/code> function. This is the reason why we need <code>Box<\/code>: it only works on Swift classes. <code>NSMutableData<\/code> is an Objective-C class, and then the function doesn't work.<\/p>\n<p>[&#8230;]<\/p>\n<p>Now we have full copy semantics, and efficient behavior: only when a copy is really necessary, it is made. This way, we never have to remember to write <code>data.copy()<\/code> ourselves, we implemented it correctly once and can then forget about it.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Chris Eidhof: Instead, we can change our mutableData accessor to prevent copying when there are no other references to the data variable. To do this, we need to check if the data is uniquely referenced. In other words, we only make a copy of the data if the data is shared. We can do this [&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,46,30,71,901],"class_list":["post-12187","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-ios","tag-languagedesign","tag-mac","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12187","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=12187"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12187\/revisions"}],"predecessor-version":[{"id":12188,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/12187\/revisions\/12188"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=12187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=12187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=12187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}