{"id":16832,"date":"2017-01-06T15:09:23","date_gmt":"2017-01-06T20:09:23","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=16832"},"modified":"2017-01-07T08:19:35","modified_gmt":"2017-01-07T13:19:35","slug":"unsafe-swift-using-pointers-and-interacting-with-c","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/01\/06\/unsafe-swift-using-pointers-and-interacting-with-c\/","title":{"rendered":"Unsafe Swift: Using Pointers and Interacting With C"},"content":{"rendered":"<p><a href=\"https:\/\/www.raywenderlich.com\/148569\/unsafe-swift\">Ray Fix<\/a> (via <a href=\"https:\/\/twitter.com\/gregheo\/status\/816787564115869696\">Greg Heo<\/a>):<\/p>\n<blockquote cite=\"https:\/\/www.raywenderlich.com\/148569\/unsafe-swift\"><p><code>MemoryLayout&lt;Type&gt;<\/code> is a generic type evaluated at compile time that determines the <code>size<\/code>, <code>alignment<\/code> and <code>stride<\/code> of each specified <code>Type<\/code>.<\/p><p>[&#8230;]<\/p><p>Unsafe Swift pointers use a very predictable naming scheme so that you know what the traits of the pointer are. Mutable or immutable, raw or typed, buffer style or not. In total there is a combination of eight of these.<\/p><p>[&#8230;]<\/p><p>This example is similar to the previous one, except that it first creates a raw pointer. The typed pointer is created by <em>binding<\/em> the memory to the required type <code>Int<\/code>. By binding memory, it can be accessed in a type-safe way. Memory binding is done behind the scenes when you create a typed pointer.<\/p><p>[&#8230;]<\/p><p>Never bind memory to two unrelated types at once. This is called <em>Type Punning<\/em> and Swift does not like puns. Instead, you can temporarily rebind memory with a method like <code>withMemoryRebound(to:capacity:)<\/code>. Also, the rules say it is illegal to rebind from a <a href=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/master\/proposals\/0107-unsaferawpointer.md#trivial-types\">trivial type<\/a> (such as an <code>Int<\/code>) to a non-trivial type (such as a <code>class<\/code>). Don&rsquo;t do it.<\/p><\/blockquote>\n<p>Previously: <a href=\"http:\/\/mjtsai.com\/blog\/2016\/11\/08\/passing-an-array-of-strings-from-swift-to-c\/\">Passing an Array of Strings From Swift to C<\/a>, <a href=\"http:\/\/mjtsai.com\/blog\/2016\/10\/05\/swift-3-0-unsafe-world\/\">Swift 3.0 Unsafe World<\/a>, <a href=\"http:\/\/mjtsai.com\/blog\/2015\/12\/02\/swift-and-c-libraries\/\">Swift and C Libraries<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Ray Fix (via Greg Heo): MemoryLayout&lt;Type&gt; is a generic type evaluated at compile time that determines the size, alignment and stride of each specified Type.[&#8230;]Unsafe Swift pointers use a very predictable naming scheme so that you know what the traits of the pointer are. Mutable or immutable, raw or typed, buffer style or not. In [&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,31,46,30,71,901],"class_list":["post-16832","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-c","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\/16832","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=16832"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16832\/revisions"}],"predecessor-version":[{"id":16835,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16832\/revisions\/16835"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=16832"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=16832"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=16832"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}