{"id":9306,"date":"2014-08-01T14:10:29","date_gmt":"2014-08-01T18:10:29","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=9306"},"modified":"2014-08-01T14:10:30","modified_gmt":"2014-08-01T18:10:30","slug":"swift-retainrelease-and-pointer-arithmetic","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2014\/08\/01\/swift-retainrelease-and-pointer-arithmetic\/","title":{"rendered":"Swift Retain\/Release and Pointer Arithmetic"},"content":{"rendered":"<p><a href=\"http:\/\/www.russbishop.net\/swift-manual-retain-release\">Russ Bishop<\/a> (via <a href=\"https:\/\/delicious.com\/rentzsch\">Wolf Rentzsch<\/a>):<\/p>\n<blockquote cite=\"http:\/\/www.russbishop.net\/swift-manual-retain-release\"><p>While creating ThreadLocalSlot&lt;T&gt;, I needed to call the posix <code>posix_getspecific<\/code> and <code>posix_setspecific<\/code> methods. Not only that, I needed to store an Objective-C object in a plain old <code>void*<\/code>. How can we accomplish that in Swift? <\/p>\n<p>The key is <code>Unmanaged<\/code>. It contains magic sauce to let us turn an <code>AnyObject<\/code> into a <code>COpaquePointer<\/code>, which is implicitly convertible to most of the other C-pointer types in Swift. It also lets us apply <code>retain<\/code> or <code>release<\/code> to an object arbitrarily. Granted, you can certainly shoot yourself in the foot, but we <em>all<\/em> lived dangerously before ARC. <\/p>\n<\/blockquote>\n<p><a href=\"http:\/\/www.russbishop.net\/swift-pointer-arithmetic\">Russ Bishop<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.russbishop.net\/swift-pointer-arithmetic\"><p>You might end up kicking yourself, but <code>UnsafePointer&lt;T&gt;<\/code> and its cousins all have arithmetic operators already defined so pointer manipulation is very simple:<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Russ Bishop (via Wolf Rentzsch): While creating ThreadLocalSlot&lt;T&gt;, I needed to call the posix posix_getspecific and posix_setspecific methods. Not only that, I needed to store an Objective-C object in a plain old void*. How can we accomplish that in Swift? The key is Unmanaged. It contains magic sauce to let us turn an AnyObject into [&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":[571,71,901],"class_list":["post-9306","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-memory-management","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9306","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=9306"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9306\/revisions"}],"predecessor-version":[{"id":9307,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9306\/revisions\/9307"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=9306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=9306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=9306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}