{"id":1923,"date":"2015-08-23T18:43:08","date_gmt":"2015-08-24T00:43:08","guid":{"rendered":"http:\/\/jamesonquave.com\/blog\/?p=1923"},"modified":"2015-09-01T14:21:33","modified_gmt":"2015-09-01T20:21:33","slug":"objective-c-pointers-and-swift","status":"publish","type":"post","link":"https:\/\/jamesonquave.com\/blog\/objective-c-pointers-and-swift\/","title":{"rendered":"Objective-C Pointers and Swift 2: A Simple Guide"},"content":{"rendered":"<p><b><i>This post written on August 23, 2015 to be compatible with Xcode 7 Beta and Swift 2<\/i><\/b><\/p>\n<h2 id=\"objective-c-pointers-and-swift\">Objective-C Pointers and Swift<\/h2>\n<p><!-- This post is intended to be a brief introduction to how you can practically work with the C pointers that crop up from time to time when working with Swift. Because the Apple SDK's are all still written in Objective-C, these pointer objects show up, and it's important to know simple ways to work with them. In this post we'll go over the basics of how to read and write to C pointers from Swift. --><\/p>\n<h3 id=\"reading-and-using-c-pointers-from-swift\">Reading C pointers from Swift<\/h3>\n<p>Let&#8217;s say I have a C Int object as a Swift pointer.<\/p>\n<p>For example this Objective-C method would return an int pointer, or (int *) in C terminology:<\/p>\n<pre><code>\r\n<div id=\"highlighter_992556\" class=\"syntaxhighlighter nogutter highlightedCode \">\r\n<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\r\n<tbody>\r\n<tr>\r\n<td class=\"code\">\r\n<div class=\"container\">\r\n<div class=\"line number1 index0 alt2\"><code class=\"keyword\">@interface<\/code> <code class=\"color2\">PointerBridge<\/code> <code class=\"plain\">: <\/code><code class=\"color1\">NSObject<\/code> <code class=\"plain\">{<\/code><\/div>\r\n<div class=\"line number2 index1 alt1\"><code class=\"undefined spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"plain\">int count;<\/code><\/div>\r\n<div class=\"line number3 index2 alt2\"><code class=\"plain\">}<\/code><\/div>\r\n<div class=\"line number4 index3 alt1\"><code class=\"plain\">- (int *) getCountPtr;<\/code><\/div>\r\n<div class=\"line number5 index4 alt2\"><code class=\"keyword\">@end<\/code><\/div>\r\n<div class=\"line number6 index5 alt1\">&nbsp;<\/div>\r\n<div class=\"line number7 index6 alt2\"><code class=\"keyword\">@implementation<\/code> <code class=\"color2\">PointerBridge<\/code><\/div>\r\n<div class=\"line number8 index7 alt1\"><code class=\"plain\">- (instancetype) <\/code><code class=\"functions\">init<\/code> <code class=\"plain\">{<\/code><\/div>\r\n<div class=\"line number9 index8 alt2\"><code class=\"undefined spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"keyword\">self<\/code> <code class=\"plain\">= [<\/code><code class=\"keyword\">super<\/code> <code class=\"functions\">init<\/code><code class=\"plain\">];<\/code><\/div>\r\n<div class=\"line number10 index9 alt1\"><code class=\"undefined spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"keyword\">if<\/code><code class=\"plain\">(<\/code><code class=\"keyword\">self<\/code><code class=\"plain\">) {<\/code><\/div>\r\n<div class=\"line number11 index10 alt2\"><code class=\"undefined spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"plain\">count = 23;<\/code><\/div>\r\n<div class=\"line number12 index11 alt1\"><code class=\"undefined spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"plain\">}<\/code><\/div>\r\n<div class=\"line number13 index12 alt2\"><code class=\"undefined spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"keyword\">return<\/code> <code class=\"keyword\">self<\/code><code class=\"plain\">;<\/code><\/div>\r\n<div class=\"line number14 index13 alt1\"><code class=\"plain\">}<\/code><\/div>\r\n<div class=\"line number15 index14 alt2\"><code class=\"plain\">- (int *) getCountPtr {<\/code><\/div>\r\n<div class=\"line number16 index15 alt1\"><code class=\"undefined spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"keyword\">return<\/code> <code class=\"plain\">&amp;count;<\/code><\/div>\r\n<div class=\"line number17 index16 alt2\"><code class=\"plain\">}<\/code><\/div>\r\n<div class=\"line number18 index17 alt1\"><code class=\"keyword\">@end<\/code><\/div>\r\n<\/div><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/div>\r\n<\/code><\/pre>\n<p>If you&#8217;re unfamiliar with Obj-C or C syntax, the above basically just declares a class called PointerBridge, and it has a single method called getCountPtr, which returns <em>the memory address<\/em> of an int whose value is 23. The Int is just a variable on the instance called <em>count<\/em>, which gets set to 23 in the constructor, init.<\/p>\n<p>I put this in an Objective-C h-file and import it in to my bridging header in order to expose it to Swift. Then in Swift I create the PointerBridge class as a new instance called bridge, and get the result of the getCountPtr() method&#8230;<\/p>\n<pre><code class=\"lang-swift\">\r\n<div id=\"highlighter_156045\" class=\"syntaxhighlighter nogutter highlightedCode \">\r\n<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\r\n<tbody>\r\n<tr>\r\n<td class=\"code\">\r\n<div class=\"container\">\r\n<div class=\"line number1 index0 alt2\"><code class=\"keyword\">let<\/code> <code class=\"plain\">bridge = <\/code><code class=\"color2\">PointerBridge<\/code><code class=\"plain\">()<\/code><\/div>\r\n<div class=\"line number2 index1 alt1\"><code class=\"keyword\">let<\/code> <code class=\"plain\">theInt = bridge.getCountPtr()<\/code><\/div>\r\n<div class=\"line number3 index2 alt2\"><code class=\"functions\">print<\/code><code class=\"plain\">(theInt)<\/code><\/div>\r\n<div class=\"line number4 index3 alt1\"><code class=\"functions\">print<\/code><code class=\"plain\">(theInt.memory)<\/code><\/div>\r\n<\/div><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/div>\r\n<\/code><\/pre>\n<p>Examine the type of <em>theInt<\/em> by holding the Option key and clicking the variable name in Xcode, and you&#8217;ll find it&#8217;s Swift type is a <em>UnsafeMutablePointer&lt;Int32&gt;<\/em>. This is a <em>pointer<\/em> to an Int, which is not the same thing as being an Int&#8230; It just <em>points<\/em> to it.<\/p>\n<p>If we run this small program and allow this Swift to execute, we&#8217;ll see <em>theInt<\/em> printed to the console as a memory address, something like <em>0x00007f8bdb508ef8<\/em>. Then, we&#8217;ll see the value 23 printed from the <em>memory<\/em> member. Accessing <em>memory<\/em> on a pointer generally returns the underlying object, in this case just the original 32-bit int (brought in to Swift as an Int32)<\/p>\n<p>Now consider our Objective-C class also allows us to set the value of count.<\/p>\n<pre><code>\r\n<div id=\"highlighter_529469\" class=\"syntaxhighlighter nogutter highlightedCode \">\r\n<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\r\n<tbody>\r\n<tr>\r\n<td class=\"code\">\r\n<div class=\"container\">\r\n<div class=\"line number1 index0 alt2\"><code class=\"keyword\">@interface<\/code> <code class=\"color2\">PointerBridge<\/code> <code class=\"plain\">: <\/code><code class=\"color1\">NSObject<\/code> <code class=\"plain\">{<\/code><\/div>\r\n<div class=\"line number2 index1 alt1\"><code class=\"undefined spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"plain\">int count;<\/code><\/div>\r\n<div class=\"line number3 index2 alt2\"><code class=\"plain\">}<\/code><\/div>\r\n<div class=\"line number4 index3 alt1\"><code class=\"plain\">- (int *) getCountPtr;<\/code><\/div>\r\n<div class=\"line number5 index4 alt2\"><code class=\"plain\">- (void) setCount:(int)newCount;<\/code><\/div>\r\n<div class=\"line number6 index5 alt1\"><code class=\"keyword\">@end<\/code><\/div>\r\n<div class=\"line number7 index6 alt2\">&nbsp;<\/div>\r\n<div class=\"line number8 index7 alt1\"><code class=\"keyword\">@implementation<\/code> <code class=\"color2\">PointerBridge<\/code><\/div>\r\n<div class=\"line number9 index8 alt2\"><code class=\"plain\">- (instancetype) <\/code><code class=\"functions\">init<\/code> <code class=\"plain\">{<\/code><\/div>\r\n<div class=\"line number10 index9 alt1\"><code class=\"undefined spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"keyword\">self<\/code> <code class=\"plain\">= [<\/code><code class=\"keyword\">super<\/code> <code class=\"functions\">init<\/code><code class=\"plain\">];<\/code><\/div>\r\n<div class=\"line number11 index10 alt2\"><code class=\"undefined spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"keyword\">if<\/code><code class=\"plain\">(<\/code><code class=\"keyword\">self<\/code><code class=\"plain\">) {<\/code><\/div>\r\n<div class=\"line number12 index11 alt1\"><code class=\"undefined spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"plain\">count = 23;<\/code><\/div>\r\n<div class=\"line number13 index12 alt2\"><code class=\"undefined spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"plain\">}<\/code><\/div>\r\n<div class=\"line number14 index13 alt1\"><code class=\"undefined spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"keyword\">return<\/code> <code class=\"keyword\">self<\/code><code class=\"plain\">;<\/code><\/div>\r\n<div class=\"line number15 index14 alt2\"><code class=\"plain\">}<\/code><\/div>\r\n<div class=\"line number16 index15 alt1\"><code class=\"plain\">- (int *) getCountPtr {<\/code><\/div>\r\n<div class=\"line number17 index16 alt2\"><code class=\"undefined spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"keyword\">return<\/code> <code class=\"plain\">&amp;count;<\/code><\/div>\r\n<div class=\"line number18 index17 alt1\"><code class=\"plain\">}<\/code><\/div>\r\n<div class=\"line number19 index18 alt2\"><code class=\"plain\">- (void) setCount:(int)newCount {<\/code><\/div>\r\n<div class=\"line number20 index19 alt1\"><code class=\"undefined spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"plain\">count = newCount;<\/code><\/div>\r\n<div class=\"line number21 index20 alt2\"><code class=\"plain\">}<\/code><\/div>\r\n<div class=\"line number22 index21 alt1\"><code class=\"keyword\">@end<\/code><\/div>\r\n<\/div><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/div>\r\n<\/code><\/pre>\n<p>Now, we could modify the value for count by calling setCount(). Since <em>theInt<\/em> is a pointer, updating the count value using setCount should also update <em>theInt.memory<\/em>. The memory address won&#8217;t change after all, just the value.<\/p>\n<p>In other words, the following should log to the console the number 23, and then the number 1000.<\/p>\n<pre><code class=\"lang-swift\">\r\n<div id=\"highlighter_952365\" class=\"syntaxhighlighter nogutter highlightedCode \">\r\n<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\r\n<tbody>\r\n<tr>\r\n<td class=\"code\">\r\n<div class=\"container\">\r\n<div class=\"line number1 index0 alt2\"><code class=\"keyword\">let<\/code> <code class=\"plain\">bridge = <\/code><code class=\"color2\">PointerBridge<\/code><code class=\"plain\">()<\/code><\/div>\r\n<div class=\"line number2 index1 alt1\"><code class=\"keyword\">let<\/code> <code class=\"plain\">theInt = bridge.getCountPtr()<\/code><\/div>\r\n<div class=\"line number3 index2 alt2\"><code class=\"functions\">print<\/code><code class=\"plain\">(theInt.memory) <\/code><code class=\"comments\">\/\/ 23<\/code><\/div>\r\n<div class=\"line number4 index3 alt1\"><code class=\"plain\">bridge.setCount(1000)<\/code><\/div>\r\n<div class=\"line number5 index4 alt2\"><code class=\"functions\">print<\/code><code class=\"plain\">(theInt.memory) <\/code><code class=\"comments\">\/\/ 1000<\/code><\/div>\r\n<\/div><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/div>\r\n<\/code><\/pre>\n<p>A shortcut you might want to take, to avoid writing .memory all the time, is to assign .memory to a variable:<\/p>\n<pre><code class=\"lang-swift\">\r\n<div id=\"highlighter_659558\" class=\"syntaxhighlighter nogutter highlightedCode \">\r\n<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\r\n<tbody>\r\n<tr>\r\n<td class=\"code\">\r\n<div class=\"container\">\r\n<div class=\"line number1 index0 alt2\"><code class=\"keyword\">let<\/code> <code class=\"plain\">bridge = <\/code><code class=\"color2\">PointerBridge<\/code><code class=\"plain\">()<\/code><\/div>\r\n<div class=\"line number2 index1 alt1\"><code class=\"keyword\">let<\/code> <code class=\"plain\">theInt = bridge.getCountPtr()<\/code><\/div>\r\n<div class=\"line number3 index2 alt2\"><code class=\"keyword\">let<\/code> <code class=\"plain\">countVal = theInt.memory<\/code><\/div>\r\n<div class=\"line number4 index3 alt1\"><code class=\"functions\">print<\/code><code class=\"plain\">(countVal) <\/code><code class=\"comments\">\/\/ 23<\/code><\/div>\r\n<\/div><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/div>\r\n<\/code><\/pre>\n<p>As before, 23 is printed to the console. However, if I do the same thing we did before, and modify the underlying count value by calling setCount(), we see a problem crop up:<\/p>\n<pre><code class=\"lang-swift\">\r\n<div id=\"highlighter_147744\" class=\"syntaxhighlighter nogutter highlightedCode \">\r\n<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\r\n<tbody>\r\n<tr>\r\n<td class=\"code\">\r\n<div class=\"container\">\r\n<div class=\"line number1 index0 alt2\"><code class=\"keyword\">let<\/code> <code class=\"plain\">bridge = <\/code><code class=\"color2\">PointerBridge<\/code><code class=\"plain\">()<\/code><\/div>\r\n<div class=\"line number2 index1 alt1\"><code class=\"keyword\">let<\/code> <code class=\"plain\">theInt = bridge.getCountPtr()<\/code><\/div>\r\n<div class=\"line number3 index2 alt2\"><code class=\"keyword\">let<\/code> <code class=\"plain\">countVal = theInt.memory<\/code><\/div>\r\n<div class=\"line number4 index3 alt1\"><code class=\"functions\">print<\/code><code class=\"plain\">(countVal) <\/code><code class=\"comments\">\/\/ 23<\/code><\/div>\r\n<div class=\"line number5 index4 alt2\">&nbsp;<\/div>\r\n<div class=\"line number6 index5 alt1\"><code class=\"plain\">bridge.setCount(1000)<\/code><\/div>\r\n<div class=\"line number7 index6 alt2\"><code class=\"functions\">print<\/code><code class=\"plain\">(countVal) <\/code><code class=\"comments\">\/\/ 23<\/code><\/div>\r\n<\/div><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/div>\r\n<\/code><\/pre>\n<p>What&#8217;s happening here is that countVal is being assigned by <em>value<\/em>. At the time of assignment the <em>value<\/em> is 23, so countVal actually has it&#8217;s own memory address where it stores 23 permanently, thus losing it&#8217;s pointer nature. <em>countVal<\/em> is just a regular Int32 now.<\/p>\n<h3 id=\"creating-c-pointers-from-swift-objects\">Creating C pointers from Swift Objects<\/h3>\n<p>What if we wanted to do the inverse of what we did above? Instead of setting the value of count with an Int, what if we needed to pass in a pointer?<\/p>\n<p>So in the Objective-C let&#8217;s say there was a method like this:<\/p>\n<pre><code>\r\n<div id=\"highlighter_756453\" class=\"syntaxhighlighter nogutter highlightedCode \">\r\n<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\r\n<tbody>\r\n<tr>\r\n<td class=\"code\">\r\n<div class=\"container\">\r\n<div class=\"line number1 index0 alt2\"><code class=\"plain\">- (void) setCountPtr:(int *)newCountPtr {<\/code><\/div>\r\n<div class=\"line number2 index1 alt1\"><code class=\"undefined spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"plain\">count = *newCountPtr;<\/code><\/div>\r\n<div class=\"line number3 index2 alt2\"><code class=\"plain\">}<\/code><\/div>\r\n<\/div><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/div>\r\n<\/code><\/pre>\n<p>This method is pretty contrived, and just ends up re-assigning the value of the newCountPtr, but in your Swift development you will face real-world scenarios of needing to pass in pointers. This one is contrived simply to demonstrate how to create the pointer type from Swift, and pass it in.<\/p>\n<p>You might think you could simply pass in an Int value, with a reference operator like &amp;. This is how you might do this same thing in C. Or for example in Objective-C you would write this:<\/p>\n<pre><code>\r\n<div id=\"highlighter_454850\" class=\"syntaxhighlighter nogutter highlightedCode \">\r\n<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\r\n<tbody>\r\n<tr>\r\n<td class=\"code\">\r\n<div class=\"container\">\r\n<div class=\"line number1 index0 alt2\"><code class=\"plain\">int mcount = 500;<\/code><\/div>\r\n<div class=\"line number2 index1 alt1\"><code class=\"plain\">[<\/code><code class=\"keyword\">self<\/code> <code class=\"plain\">setCountPtr:&amp;mcount];<\/code><\/div>\r\n<\/div><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/div>\r\n<\/code><\/pre>\n<p>Which would update the count value to be 500 successfully. However in Swift, you may see just from auto-complete that it wants something a little more complicated (and verbose). It&#8217;s looking for a newCountPtr of type UnsafeMutablePointer&lt;Int32&gt;.<\/p>\n<p>I know this type is a mouthful, and it looks really complicated. However, it&#8217;s actually fairly simple, especially if you know pointers from Obj-C. In order to create an object with type UnsafeMutablePointer&lt;Int32&gt;, we just call the constructor and for it&#8217;s only argument we pass in the size of the pointer in units of the number of objects.<\/p>\n<pre><code class=\"lang-swift\">\r\n<div id=\"highlighter_671130\" class=\"syntaxhighlighter nogutter highlightedCode \">\r\n<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\r\n<tbody>\r\n<tr>\r\n<td class=\"code\">\r\n<div class=\"container\">\r\n<div class=\"line number1 index0 alt2\"><code class=\"keyword\">let<\/code> <code class=\"plain\">bridge = <\/code><code class=\"color2\">PointerBridge<\/code><code class=\"plain\">()<\/code><\/div>\r\n<div class=\"line number2 index1 alt1\"><code class=\"keyword\">let<\/code> <code class=\"plain\">theInt = bridge.getCountPtr()<\/code><\/div>\r\n<div class=\"line number3 index2 alt2\"><code class=\"functions\">print<\/code><code class=\"plain\">(theInt.memory) <\/code><code class=\"comments\">\/\/ 23<\/code><\/div>\r\n<div class=\"line number4 index3 alt1\">&nbsp;<\/div>\r\n<div class=\"line number5 index4 alt2\"><code class=\"keyword\">let<\/code> <code class=\"plain\">newIntPtr = <\/code><code class=\"color2\">UnsafeMutablePointer<\/code><code class=\"plain\">&lt;<\/code><code class=\"color2\">Int32<\/code><code class=\"plain\">&gt;.alloc(1)<\/code><\/div>\r\n<div class=\"line number6 index5 alt1\"><code class=\"plain\">newIntPtr.memory = 100<\/code><\/div>\r\n<div class=\"line number7 index6 alt2\"><code class=\"plain\">bridge.setCountPtr(newIntPtr)<\/code><\/div>\r\n<div class=\"line number8 index7 alt1\">&nbsp;<\/div>\r\n<div class=\"line number9 index8 alt2\"><code class=\"functions\">print<\/code><code class=\"plain\">(theInt.memory) <\/code><code class=\"comments\">\/\/ 100<\/code><\/div>\r\n<\/div><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/div>\r\n<\/code><\/pre>\n<p>The only argument to the UnsafeMutablePointer&lt;Int32&gt; constructor is how many objects worth of space will be allocated, so we can just pass in 1 since we only need 1 Int32 object. Then, simply inverting what we did before with the <em>memory<\/em> property, we can assign a value to our newly created pointer. Finally, we can simply pass in the newIntPtr to the setCountrPtr method. Printing out the value of the original <em>theInt<\/em> pointer, we see the value has indeed been updated to 100.<\/p>\n<h3 id=\"conclusion\">Conclusion<\/h3>\n<p>The UnsafeMutablePointer&lt;T&gt; type, and it&#8217;s sibling UnsafePointer&lt;T&gt; are basically just abstractions of C pointers. If it helps to wrap your head around them, think of them sort of like Swift Optionals, in that they do not directly equate to a certain value, but instead act as an abstraction layer on top of a contained variable. The type is generic, and allows for other types to be used than just Int32. For example if you needed to work with a Float object you might need an UnsafeMutablePointer&lt;Float&gt;.<\/float><\/p>\n<p>The important thing to remember is that you do not <em>cast<\/em> from an Int to an UnsafeMutablePointer&lt;Int&gt;, because a pointer is simply not the same as an Int value. So instead, be prepared to create a new object, calling it&#8217;s constructor <code>UnsafeMutablePointer&lt;Int&gt;(count: Int)<\/code>.<\/int><\/p>\n<p>\nWe&#8217;ll follow-up this post with a deeper dive in to the details of function pointers, and how to take advantage of these features to work with legacy C and Objective-C APIs. Make sure to <A href=\"http:\/\/eepurl.com\/sDFL9\">sign up for our newsletter so you don&#8217;t miss it<\/a>!<a href=\"http:\/\/eepurl.com\/sDFL9\"><\/p>\n<h1>Subscribe For Free Now &raquo;<\/h1>\n<p><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post written on August 23, 2015 to be compatible with Xcode 7 Beta and Swift 2 Objective-C Pointers and Swift Reading C pointers from Swift Let&#8217;s say I have a C Int object as a Swift pointer. For example this Objective-C method would return an int pointer, or (int *) in C terminology: @interface&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_links_to":"","_links_to_target":""},"categories":[1],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.13 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Objective-C Pointers and Swift 2: A Simple Guide - Jameson Quave<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jamesonquave.com\/blog\/objective-c-pointers-and-swift\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Objective-C Pointers and Swift 2: A Simple Guide - Jameson Quave\" \/>\n<meta property=\"og:description\" content=\"This post written on August 23, 2015 to be compatible with Xcode 7 Beta and Swift 2 Objective-C Pointers and Swift Reading C pointers from Swift Let&#8217;s say I have a C Int object as a Swift pointer. For example this Objective-C method would return an int pointer, or (int *) in C terminology: @interface...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jamesonquave.com\/blog\/objective-c-pointers-and-swift\/\" \/>\n<meta property=\"og:site_name\" content=\"Jameson Quave\" \/>\n<meta property=\"article:published_time\" content=\"2015-08-24T00:43:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-09-01T20:21:33+00:00\" \/>\n<meta name=\"author\" content=\"Jameson Quave\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jameson Quave\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/objective-c-pointers-and-swift\/\",\"url\":\"https:\/\/jamesonquave.com\/blog\/objective-c-pointers-and-swift\/\",\"name\":\"Objective-C Pointers and Swift 2: A Simple Guide - Jameson Quave\",\"isPartOf\":{\"@id\":\"https:\/\/jamesonquave.com\/blog\/#website\"},\"datePublished\":\"2015-08-24T00:43:08+00:00\",\"dateModified\":\"2015-09-01T20:21:33+00:00\",\"author\":{\"@id\":\"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/db6184f355c7f4e3b876d0f228c2fcfc\"},\"breadcrumb\":{\"@id\":\"https:\/\/jamesonquave.com\/blog\/objective-c-pointers-and-swift\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jamesonquave.com\/blog\/objective-c-pointers-and-swift\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/objective-c-pointers-and-swift\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jamesonquave.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Objective-C Pointers and Swift 2: A Simple Guide\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/#website\",\"url\":\"https:\/\/jamesonquave.com\/blog\/\",\"name\":\"Jameson Quave\",\"description\":\"Using computer technology to educate, and improve lives.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jamesonquave.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/db6184f355c7f4e3b876d0f228c2fcfc\",\"name\":\"Jameson Quave\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d9786c83345117d560bbeab0e1f26814?s=96&d=retro&r=pg\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d9786c83345117d560bbeab0e1f26814?s=96&d=retro&r=pg\",\"caption\":\"Jameson Quave\"},\"sameAs\":[\"http:\/\/jamesonquave.com\"],\"url\":\"https:\/\/jamesonquave.com\/blog\/author\/jquave\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Objective-C Pointers and Swift 2: A Simple Guide - Jameson Quave","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jamesonquave.com\/blog\/objective-c-pointers-and-swift\/","og_locale":"en_US","og_type":"article","og_title":"Objective-C Pointers and Swift 2: A Simple Guide - Jameson Quave","og_description":"This post written on August 23, 2015 to be compatible with Xcode 7 Beta and Swift 2 Objective-C Pointers and Swift Reading C pointers from Swift Let&#8217;s say I have a C Int object as a Swift pointer. For example this Objective-C method would return an int pointer, or (int *) in C terminology: @interface...","og_url":"https:\/\/jamesonquave.com\/blog\/objective-c-pointers-and-swift\/","og_site_name":"Jameson Quave","article_published_time":"2015-08-24T00:43:08+00:00","article_modified_time":"2015-09-01T20:21:33+00:00","author":"Jameson Quave","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Jameson Quave","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/jamesonquave.com\/blog\/objective-c-pointers-and-swift\/","url":"https:\/\/jamesonquave.com\/blog\/objective-c-pointers-and-swift\/","name":"Objective-C Pointers and Swift 2: A Simple Guide - Jameson Quave","isPartOf":{"@id":"https:\/\/jamesonquave.com\/blog\/#website"},"datePublished":"2015-08-24T00:43:08+00:00","dateModified":"2015-09-01T20:21:33+00:00","author":{"@id":"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/db6184f355c7f4e3b876d0f228c2fcfc"},"breadcrumb":{"@id":"https:\/\/jamesonquave.com\/blog\/objective-c-pointers-and-swift\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jamesonquave.com\/blog\/objective-c-pointers-and-swift\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jamesonquave.com\/blog\/objective-c-pointers-and-swift\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jamesonquave.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Objective-C Pointers and Swift 2: A Simple Guide"}]},{"@type":"WebSite","@id":"https:\/\/jamesonquave.com\/blog\/#website","url":"https:\/\/jamesonquave.com\/blog\/","name":"Jameson Quave","description":"Using computer technology to educate, and improve lives.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jamesonquave.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/db6184f355c7f4e3b876d0f228c2fcfc","name":"Jameson Quave","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d9786c83345117d560bbeab0e1f26814?s=96&d=retro&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d9786c83345117d560bbeab0e1f26814?s=96&d=retro&r=pg","caption":"Jameson Quave"},"sameAs":["http:\/\/jamesonquave.com"],"url":"https:\/\/jamesonquave.com\/blog\/author\/jquave\/"}]}},"_links":{"self":[{"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/posts\/1923"}],"collection":[{"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/comments?post=1923"}],"version-history":[{"count":11,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/posts\/1923\/revisions"}],"predecessor-version":[{"id":1935,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/posts\/1923\/revisions\/1935"}],"wp:attachment":[{"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/media?parent=1923"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/categories?post=1923"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/tags?post=1923"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}