<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jul 29, 2016 at 8:40 PM, Chris McIntyre via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word">Hi guys, <div><br></div><div>I’m having issues with Swift pointers. I feel like the Interactive With C APis document only gets you half way there. </div><div><br></div><div>For example, look at this from the docs</div><div><br></div><p style="border:0px;font-size:1.4em;margin:0px 0px 15px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(65,65,65);line-height:20.3px;font-family:helvetica,arial,sans-serif">If you have declared a function like this one:</p><div style="border:0px;font-size:1.4em;margin:0px 0px 0px 21px;outline:0px;padding:0px;vertical-align:baseline"><div style="border-width:0px 0px 0px 5px;border-left-style:solid;border-left-color:rgb(205,233,244);margin:10px 0px 21px 13px;outline:0px;padding:4px 0px 2px 10px;vertical-align:baseline;width:616.578px"><ol style="border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;line-height:1.6em;list-style:none"><li style="border-width:0px 0px 0px 18px;border-left-style:solid;border-left-color:transparent;margin:0px;outline:0px;padding:0px 10px 0px 0px;vertical-align:baseline;white-space:pre-wrap;list-style-type:none"><code style="border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;font-family:menlo,monospace;word-wrap:break-word"><span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(170,51,145)">func</span> <span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116)">takesAMutablePointer</span>(<span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116)">x</span>: <span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(92,38,153)">UnsafeMutablePointer</span>&lt;<span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(92,38,153)">Float</span>&gt;) {</code></li><li style="border-width:0px 0px 0px 18px;border-left-style:solid;border-left-color:transparent;margin:0px;outline:0px;padding:0px 10px 0px 0px;vertical-align:baseline;white-space:pre-wrap;list-style-type:none"><code style="border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;font-family:menlo,monospace;word-wrap:break-word">    <span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(0,131,18)">// ...</span></code></li><li style="border-width:0px 0px 0px 18px;border-left-style:solid;border-left-color:transparent;margin:0px;outline:0px;padding:0px 10px 0px 0px;vertical-align:baseline;white-space:pre-wrap;list-style-type:none"><code style="border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;font-family:menlo,monospace;word-wrap:break-word">}</code></li></ol></div></div><p style="border:0px;font-size:1.4em;margin:0px 0px 15px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(65,65,65);line-height:20.3px;font-family:helvetica,arial,sans-serif">You can call it in any of the following ways:</p><div style="border:0px;font-size:1.4em;margin:0px 0px 0px 21px;outline:0px;padding:0px;vertical-align:baseline"><div style="border-width:0px 0px 0px 5px;border-left-style:solid;border-left-color:rgb(205,233,244);margin:10px 0px 21px 13px;outline:0px;padding:4px 0px 2px 10px;vertical-align:baseline;width:616.578px"><ol style="border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;line-height:1.6em;list-style:none"><li style="border-width:0px 0px 0px 18px;border-left-style:solid;border-left-color:transparent;margin:0px;outline:0px;padding:0px 10px 0px 0px;vertical-align:baseline;white-space:pre-wrap;list-style-type:none"><code style="border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;font-family:menlo,monospace;word-wrap:break-word"><span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(170,51,145)">var</span> <span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116)">x</span>: <span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(92,38,153)">Float</span> = <span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(28,0,207)">0.0</span></code></li><li style="border-width:0px 0px 0px 18px;border-left-style:solid;border-left-color:transparent;margin:0px;outline:0px;padding:0px 10px 0px 0px;vertical-align:baseline;white-space:pre-wrap;list-style-type:none"><code style="border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;font-family:menlo,monospace;word-wrap:break-word"><span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(170,51,145)">var</span> <span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116)">p</span>: <span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(92,38,153)">UnsafeMutablePointer</span>&lt;<span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(92,38,153)">Float</span>&gt; = <span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(170,51,145)">nil</span></code></li><li style="border-width:0px 0px 0px 18px;border-left-style:solid;border-left-color:transparent;margin:0px;outline:0px;padding:0px 10px 0px 0px;vertical-align:baseline;white-space:pre-wrap;list-style-type:none"><code style="border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;font-family:menlo,monospace;word-wrap:break-word"><span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(170,51,145)">var</span> <span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116)">a</span>: [<span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(92,38,153)">Float</span>] = [<span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(28,0,207)">1.0</span>, <span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(28,0,207)">2.0</span>, <span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(28,0,207)">3.0</span>]</code></li><li style="border-width:0px 0px 0px 18px;border-left-style:solid;border-left-color:transparent;margin:0px;outline:0px;padding:0px 10px 0px 0px;vertical-align:baseline;white-space:pre-wrap;list-style-type:none"><code style="border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;font-family:menlo,monospace;word-wrap:break-word"><span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116)">takesAMutablePointer</span>(<span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(170,51,145)">nil</span>)</code></li><li style="border-width:0px 0px 0px 18px;border-left-style:solid;border-left-color:transparent;margin:0px;outline:0px;padding:0px 10px 0px 0px;vertical-align:baseline;white-space:pre-wrap;list-style-type:none"><code style="border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;font-family:menlo,monospace;word-wrap:break-word"><span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116)">takesAMutablePointer</span>(<span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116)">p</span>)</code></li><li style="border-width:0px 0px 0px 18px;border-left-style:solid;border-left-color:transparent;margin:0px;outline:0px;padding:0px 10px 0px 0px;vertical-align:baseline;white-space:pre-wrap;list-style-type:none"><code style="border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;font-family:menlo,monospace;word-wrap:break-word"><span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116)">takesAMutablePointer</span>(&amp;<span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116)">x</span>)</code></li><li style="border-width:0px 0px 0px 18px;border-left-style:solid;border-left-color:transparent;margin:0px;outline:0px;padding:0px 10px 0px 0px;vertical-align:baseline;white-space:pre-wrap;list-style-type:none"><code style="border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;font-family:menlo,monospace;word-wrap:break-word"><span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116)">takesAMutablePointer</span>(&amp;<span style="border:0px;font-size:11.9px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116)">a</span>)</code></li></ol></div></div><div>Seem simple enough. But then I was trying to figure out Core Data validation, which takes an AutoreleasingUnsafeMutablePointer&lt;AnyObject?&gt; and I can’t figure out what to pass to it. </div><div><br></div><div>I tried to create a simple test in a Playground:</div><div><br></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="color:rgb(194,52,155)">func</span><span> takesAPointer(p: </span><span style="color:rgb(0,175,202)">AutoreleasingUnsafeMutablePointer</span><span style="color:rgb(255,255,255)">&lt;</span><span style="color:rgb(0,175,202)">AnyObject</span><span>?&gt;)</span>{</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(194,52,155)"><span style="color:rgb(255,255,255)">    </span><span>return</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span>}</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(255,255,255);min-height:13px"><span></span><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="color:rgb(194,52,155)">var</span><span> myString = </span><span style="color:rgb(228,68,72)">&quot;Hello&quot;</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(255,255,255);min-height:13px"><span></span><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="color:rgb(147,200,106)">takesAPointer</span><span>(p: </span><span>&amp;</span><span style="color:rgb(147,200,106)">myString</span><span>)</span></p></div></div></blockquote><div><br></div><div>Try this: </div><br>import Foundation <br>func takesAPointer(p: AutoreleasingUnsafeMutablePointer&lt;AnyObject?&gt;){   <br>    return <br>}    <br>let myString : NSString? = &quot;Hello&quot;    <br>var obj : AnyObject? = myString  <br>takesAPointer(&amp;obj)<div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><div><div><br></div></div><div>Then I get an error stating &#39;Cannot pass immutable type “AnyObject?” as inout argument’. </div><div><br></div><div>Everything seems to match the example from the docs. I have a var (so it should be mutable) and I’m using the ampersand, but still I’m getting an error. </div><div><br></div><div>Another problem. I have a specific byte pattern I want to create. For arguments sake, lets call it 0x123ABC, and I have it as an Int. I want to access the individual bytes (i.e. 12, 3A, BC). </div></div></blockquote><div><br></div><div>Is something like this what you&#39;re looking for: </div><div>let a = UInt16((0x123ABC &gt;&gt; 16) &amp; 0xFF)<br></div>







<div>let b = UInt16((0x123ABC &gt;&gt; 8) &amp; 0xFF)<br></div><div>print(String(a, radix: 16))</div><div>print(String(b, radix: 16))</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><div><br></div><div>The struct reference for UnsafePointer&lt;T&gt; doesn’t talk much about initializing it. Most of the initializers take a pointer. I tried the init(_ bitPattern:) initializer, and was able to create a pointer, but it seemed to point to the address 0x123ABC rather than the address *of* 0x123ABC. I tried creating a buffer with malloc, and it gives me an UnsafeMutablePointer  but now I can’t figure out how to copy my bytes to this buffer.</div><div><br></div><div>So clearly there’s something I’m just not grocking about Swift pointers. Does anyone know of a more remedial tutorial that is updated for Swift 3? I’d like to continue to work in pure Swift, but it just isn’t clicking.</div><div><br></div><div>--</div><div><div>Chris McIntyre<br><br><br><br>

</div><div><div class="gmail-h5">
<br><div><blockquote type="cite"><div>On Jul 29, 2016, at 6:11 AM, James Campbell via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:</div><br><div><div dir="ltr"><div style="font-family:arial,helvetica,sans-serif;font-size:small">No I haven&#39;t thats a big help thank you !</div></div><div class="gmail_extra"><br clear="all"><div><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><p><b><font color="#cc0000">___________________________________</font></b></p><p><b>James⎥Head of Trolls</b></p><p><b><font color="#cc0000"><a href="mailto:james@supmenow.com" target="_blank">james@supmenow.com</a>⎥<a href="http://supmenow.com/" target="_blank">supmenow.com</a></font></b></p><p><b><font size="2">Sup</font></b></p><p><b><font size="2">Runway East
</font></b></p><p><b><font size="2">10 Finsbury Square</font></b></p><p><b><font size="2">London</font></b></p><p><b><font size="2">
EC2A 1AF </font></b></p></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>
<br><div class="gmail_quote">On 29 July 2016 at 10:40, Zhao Xin <span dir="ltr">&lt;<a href="mailto:owenzx@gmail.com" target="_blank">owenzx@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div style="font-family:georgia,serif">Have you read <a href="https://developer.apple.com/library/tvos/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html#//apple_ref/doc/uid/TP40014216-CH8-ID17" target="_blank">https://developer.apple.com/library/tvos/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html#//apple_ref/doc/uid/TP40014216-CH8-ID17</a> ?</div><div style="font-family:georgia,serif"><br></div><div style="font-family:georgia,serif">Zhaoxin</div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div>On Fri, Jul 29, 2016 at 4:55 PM, James Campbell via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div><div><div dir="ltr"><div style="font-family:arial,helvetica,sans-serif;font-size:small">​Do you know of any resources to brush up on the pointer aspect of swift ? ​</div></div><div class="gmail_extra"><span><br clear="all"><div><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><p><b><font color="#cc0000">___________________________________</font></b></p><p><b>James⎥Head of Trolls</b></p><p><b><font color="#cc0000"><a href="mailto:james@supmenow.com" target="_blank">james@supmenow.com</a>⎥<a href="http://supmenow.com/" target="_blank">supmenow.com</a></font></b></p><p><b><font size="2">Sup</font></b></p><p><b><font size="2">Runway East
</font></b></p><p><b><font size="2">10 Finsbury Square</font></b></p><p><b><font size="2">London</font></b></p><p><b><font size="2">
EC2A 1AF </font></b></p></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>
<br></span><span><div class="gmail_quote">On 29 July 2016 at 09:10, Dmitri Gribenko <span dir="ltr">&lt;<a href="mailto:gribozavr@gmail.com" target="_blank">gribozavr@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><span>On Fri, Jul 29, 2016 at 12:55 AM, James Campbell &lt;<a href="mailto:james@supmenow.com" target="_blank">james@supmenow.com</a>&gt; wrote:<br>
&gt; So this:<br>
&gt;<br>
&gt; if let data = someArrayGeneratingFunction() {<br>
&gt;   cFunction(UnsafeMutablePointer(data))<br>
&gt; }<br>
&gt;<br>
&gt; Has issues with the array passed to c getting corrupted, but this doesn&#39;t:<br>
&gt;<br>
&gt; let data = someArrayGeneratingFunction()<br>
&gt;<br>
&gt; if let data = data {<br>
&gt;   cFunction(UnsafeMutablePointer(data))<br>
&gt; }<br>
<br>
</span>Neither piece of code is guaranteed to work.  (You are just getting<br>
lucky that the second one happens to work.)  Array-to-pointer<br>
conversion only extends the lifetime of the array until the immediate<br>
function call returns.  So after UnsafeMutablePointer(data) returns,<br>
the array can be freed.<br>
<br>
Use someArrayGeneratingFunction.withUnsafeMutableBuffer { ... } instead.<br>
<div><div><br>
Dmitri<br>
<br>
--<br>
main(i,j){for(i=2;;i++){for(j=2;j&lt;i;j++){if(!(i%j)){j=0;break;}}if<br>
(j){printf(&quot;%d\n&quot;,i);}}} /*Dmitri Gribenko &lt;<a href="mailto:gribozavr@gmail.com" target="_blank">gribozavr@gmail.com</a>&gt;*/<br>
</div></div></blockquote></div><br></span></div>
<br></div></div>_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>
</blockquote></div><br></div>
_______________________________________________<br>swift-users mailing list<br><a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br></div></blockquote></div><br></div></div></div></div><br>_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div></div>