<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">The <tt>withUnsafeMutableBytes</tt>
method has two generic parameters, <tt>ResultType</tt> and <tt>ContentType</tt>:<br>
<br>
<pre class="code-source" data-language="swift" style="background-color: rgb(250, 250, 250); border: 1px solid rgb(227, 227, 227); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; padding: 15px 18px; overflow-x: auto; line-height: 24px; white-space: pre-wrap; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;"><code style="font-family: 'SF Mono', Menlo, monospace;"><span class="syntax-keyword" style="color: rgb(170, 13, 145);">mutating</span> <span class="syntax-keyword" style="color: rgb(170, 13, 145);">func</span> <span class="syntax-identifier" style="color: rgb(0, 0, 0);">withUnsafeMutableBytes</span><<span class="syntax-identifier" style="color: rgb(0, 0, 0);">ResultType</span>, <span class="syntax-identifier" style="color: rgb(0, 0, 0);">ContentType</span>>(<span class="syntax-identifier" style="color: rgb(0, 0, 0);">_</span> <span class="syntax-param-name">body</span>: (<span class="syntax-type" style="color: rgb(92, 38, 153);"><a href="dash-apple-api://load?request_key=hsAKsXE560&language=swift" style="color: rgb(92, 38, 153); text-decoration: none;">UnsafeMutablePointer</a></span><ContentType>) <span class="syntax-keyword" style="color: rgb(170, 13, 145);">throws</span> -> ResultType) <span class="syntax-keyword" style="color: rgb(170, 13, 145);">rethrows</span> -> ResultType</code></pre>
<br>
In your examples, the type checker can't infer the type of <tt>ResultType</tt>.
You'll have to state it explicitly by specifying the type of the
closure's argument. For example:<br>
<br>
<pre>msg.withUnsafeMutableBytes {
(inPointer<b>: UnsafeMutablePointer<UInt8></b>) -> Void in
// ...
}</pre>
<br>
On 25.04.2017 10:45, Rick Mann via swift-users wrote:<br>
</div>
<blockquote
cite="mid:20420BF5-6D6C-4C54-A947-2B9B49F3BDB2@latencyzero.com"
type="cite">
<pre wrap="">The following playground reproduces an issue I'm having, in that the code won't compile depending on the content of the closure. In fact, an empty closure is fine, but when I try to call certain things, it's not.
I figure it has something to do with the type inference for inPointer, but I can't figure out what it needs to work.
---------------------------
import Foundation
//        OKAY:
var msg = Data(capacity: 123456)
msg.withUnsafeMutableBytes
{ (inPointer) -> Void in
        foo(inPointer)
}
//error: cannot convert value of type '(_) -> Void' to expected argument type '(UnsafeMutablePointer<_>) -> _'
//{ (inPointer) -> Void in
//^~~~~~~~~~~~~~~~~~~~~~~~
msg.withUnsafeMutableBytes
{ (inPointer) -> Void in
}
//error: cannot convert value of type '(_) -> Void' to expected argument type '(UnsafeMutablePointer<_>) -> _'
//{ (inPointer) -> Void in
//^~~~~~~~~~~~~~~~~~~~~~~~
msg.withUnsafeMutableBytes
{ (inPointer) -> Void in
        var s: Int
        lgs_error(inPointer, 123456, &s)
}
func
foo(_ data: UnsafeMutableRawPointer!)
{
}
func
lgs_error(_ message: UnsafeMutablePointer<Int8>!,
         _ message_capacity: Int,
         _ message_size: UnsafeMutablePointer<Int>!) -> Int
{
}
---------------------------
</pre>
</blockquote>
</body>
</html>