{"id":15944,"date":"2016-10-05T14:15:54","date_gmt":"2016-10-05T18:15:54","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=15944"},"modified":"2016-10-05T14:19:13","modified_gmt":"2016-10-05T18:19:13","slug":"swift-3-0-unsafe-world","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/10\/05\/swift-3-0-unsafe-world\/","title":{"rendered":"Swift 3.0 Unsafe World"},"content":{"rendered":"<p><a href=\"http:\/\/technology.meronapps.com\/2016\/09\/27\/swift-3-0-unsafe-world-2\/\">Roberto Perez<\/a> (via <a href=\"https:\/\/news.ycombinator.com\/item?id=12589230\">Hacker News<\/a>):<\/p>\n<blockquote cite=\"http:\/\/technology.meronapps.com\/2016\/09\/27\/swift-3-0-unsafe-world-2\/\"><p>But, what if we want to take a pointer to a Swift managed memory without having to create a function? To do it we will use <code>withUnsafeMutablePointer<\/code>, that will take a reference to a Swift type and a block with the pointer as it&rsquo;s parameter.<\/p><p>[&#8230;]<\/p><p>When dealing with C API you need sometimes to cast pointers to struct to a different struct. This is very easy to do en C (and very dangerous and error prone too), as you have seen in Swift, all pointers are <em>typed<\/em>, that means that an <code>UnsafePointer&lt;Int&gt;<\/code> cannot be used where an <code>UnsafePointer&lt;UInt8&gt;<\/code> is required, that&rsquo;s good in terms of producing a safer code, but at the same time that makes not possible to interact with C APIs that requires this types of casts, like for example socket <code>bind()<\/code> function. For theses cases, we will use <code>withMemoryRebound<\/code> which is a function that will convert a pointer from a type to a different one.<\/p><p>[&#8230;]<\/p><p>Before Swift 3.0, you could do it with <code>UnsafePointer&lt;Void&gt;<\/code> however, in 3.0 a new type has been added to handle these types of pointers: <code>UnsafeRawPointer<\/code>. This struct is not generic, so it means that it won&rsquo;t hold information tied to any specific type and that will simplifly our code.<\/p><p>[&#8230;]<\/p><p>If we construct a <code>UnsafeBufferPointer<\/code> from an <code>UnsafePointer<\/code> we will be able to use most of the array functions of native Swift type given that <code>UnsafeBufferPointer<\/code> implements <code>Collection<\/code>, <code>Indexable<\/code> and <code>RandomAccessCollection<\/code> swift protocols.<\/p><p>[&#8230;]<\/p><p>Swift has an utility to take pointers to objects <em>retaining<\/em> its reference or not depending on our needs. Those are static functions of <code>Unmanaged<\/code> struct. With <code>passRetained()<\/code> we will create a retained reference to an object, so we can be sure that when using it from C world, it will be still there. If the object is already retained for the life of the callback we can also use <code>passUnretained()<\/code>. Both methods produces a instance of <code>Unmanaged<\/code> that will be converted to a <code>UnsafeRawPointer<\/code> by calling <code>toOpaque()<\/code><\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Roberto Perez (via Hacker News): But, what if we want to take a pointer to a Swift managed memory without having to create a function? To do it we will use withUnsafeMutablePointer, that will take a reference to a Swift type and a block with the pointer as it&rsquo;s parameter.[&#8230;]When dealing with C API you [&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":[45,46,571,71,901],"class_list":["post-15944","post","type-post","status-publish","format-standard","hentry","category-programming-category","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\/15944","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=15944"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/15944\/revisions"}],"predecessor-version":[{"id":15945,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/15944\/revisions\/15945"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=15944"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=15944"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=15944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}