{"id":18628,"date":"2017-08-14T14:37:57","date_gmt":"2017-08-14T18:37:57","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=18628"},"modified":"2017-08-14T14:37:57","modified_gmt":"2017-08-14T18:37:57","slug":"swift-unmanaged","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/08\/14\/swift-unmanaged\/","title":{"rendered":"Swift.Unmanaged"},"content":{"rendered":"<p><a href=\"https:\/\/mikeash.com\/pyblog\/friday-qa-2017-08-11-swiftunmanaged.html\">Mike Ash<\/a>:<\/p>\n<blockquote cite=\"https:\/\/mikeash.com\/pyblog\/friday-qa-2017-08-11-swiftunmanaged.html\">\n<p>In order to work with C APIs, we sometimes need to convert Swift object references to and from raw pointers. [&#8230;] You can do this with <code>unsafeBitCast<\/code>, although I strongly recommend against it.<\/p>\n<p>[&#8230;]<\/p>\n<p>To create the <code>Unmanaged<\/code> value, use <code>fromOpaque<\/code>. Unlike <code>passRetained<\/code> and <code>passUnretained<\/code>, you must specify the generic type for <code>Unmanaged<\/code> when using <code>fromOpaque<\/code> so that the compiler knows which class you're expecting to receive.<\/p>\n<p>Once you have the <code>Unmanaged<\/code> value, you can get a reference out of it. Using <code>takeRetainedValue<\/code> will perform an unbalanced release, which will balance an unbalanced retain previously performed with <code>passRetained<\/code>, or by C code which confers ownership on your code. Using <code>takeUnretainedValue<\/code> will obtain the object reference without performing any retain or release.<\/p>\n<p>[&#8230;]<\/p>\n<p>To retrieve a reference from C into Swift code, you create an <code>Unmanaged<\/code> value from the raw pointer, then take a reference from it. There are two ways to take a reference: one which consumes an unbalanced retain, and once which performs no memory management.<\/p>\n<\/blockquote>\n<p>Previously: <a href=\"https:\/\/mjtsai.com\/blog\/2016\/10\/05\/swift-3-0-unsafe-world\/\">Swift 3.0 Unsafe World<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Mike Ash: In order to work with C APIs, we sometimes need to convert Swift object references to and from raw pointers. [&#8230;] You can do this with unsafeBitCast, although I strongly recommend against it. [&#8230;] To create the Unmanaged value, use fromOpaque. Unlike passRetained and passUnretained, you must specify the generic type for Unmanaged [&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":[55,45,46,571,71,901],"class_list":["post-18628","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-arc","tag-c","tag-languagedesign","tag-memory-management","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18628","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=18628"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18628\/revisions"}],"predecessor-version":[{"id":18629,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18628\/revisions\/18629"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=18628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=18628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=18628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}