title: Upload a file to FTP
body: <p>I'm trying to upload a file with Swift.
  I found this (poorly written) tutorial from Apple for Objective-C:</p>
  
  <p>I tried to implement it in Swift, but got stuck on dereferencing the pointer in the callback function:</p>
  
  <pre><code>func upload(filepath:String, url:String){
          do {
              let attr:NSDictionary? = try FileManager.default.attributesOfItem(atPath: filepath) as NSDictionary;
              if attr == nil { return; }
              let size:UInt64? = attr?.fileSize();
              if size==nil { return; }
              var info = MyStreamInfo(writeStream: CFWriteStreamCreateWithFTPURL(nil, CFURLCreateWithString(nil,url as CFString, nil)) as! CFWriteStream,
                                      readStream: CFReadStreamCreateWithFile(nil, CFURLCreateWithString(nil, filepath as CFString, nil)),
                                      /*proxyDict: CFDictionary(), */fileSize: size!, totalBytesWritten: 0, leftOverByteCount: 0, buffer: UnsafeMutablePointer&lt;UInt8&gt;.allocate(capacity: bSize));
              var myContext=CFStreamClientContext(version: 0, info: &amp;info, retain: nil, release: nil, copyDescription: nil);
              //myContext.info = info;
              CFReadStreamOpen(info.readStream);
  
              CFWriteStreamSetProperty(info.writeStream, CFStreamPropertyKey(kCFStreamPropertyFTPUserName), username as CFTypeRef);
              CFWriteStreamSetProperty(info.writeStream, CFStreamPropertyKey(kCFStreamPropertyFTPPassword), password as CFTypeRef);
              CFWriteStreamSetProperty(info.writeStream, CFStreamPropertyKey(kCFStreamPropertyFTPProxyHost), hostname as CFTypeRef);
              CFWriteStreamSetProperty(info.writeStream, CFStreamPropertyKey(kCFStreamPropertyFTPProxyPort), port as CFTypeRef);
  
              CFWriteStreamSetClient(info.writeStream, CFStreamEventType.canAcceptBytes.rawValue, writeCB as! CFWriteStreamClientCallBack, &amp;myContext);
  
  
          } catch let error as NSError  {
              print(error);
          }
  
  
  
      }
  
      func writeCB(stream:CFWriteStream, event:CFStreamEventType, myptr: UnsafeMutableRawPointer) {
          var info:MyStreamInfo;
          var totalBytesRead:Int32 = 0;
          repeat {
              var bytesRead:Int32 = 0;
              var bytesWritten:Int32 = 0;
              if info.leftOverByteCount&gt;0 {
                  bytesRead = Int32(info.leftOverByteCount);
              } else {
                  bytesRead = Int32(CFReadStreamRead(info.readStream, info.buffer, bSize));
                  if (bytesRead &lt; 0){
                      print("error");
                      return;
                  }
                  totalBytesRead += bytesRead;
              }
              bytesWritten = Int32(CFWriteStreamWrite(info.writeStream, info.buffer, CFIndex(bytesRead)));
              if bytesWritten &gt; 0 {
                  info.totalBytesWritten += UInt32(bytesWritten);
  
                  if bytesWritten &lt; bytesRead {
                      info.leftOverByteCount = UInt32(bytesRead - bytesWritten);
                      memmove(info.buffer, info.buffer.advanced(by: Int(bytesWritten)), Int(info.leftOverByteCount));
                  } else {
                      info.leftOverByteCount = 0;
                  }
              } else {
                  if bytesWritten &lt; 0  {
                      print("error");
                      break;
                  }
              }
          } while(CFWriteStreamCanAcceptBytes(info.writeStream));
  
      }
  </code></pre>
  
  <p>How can I get the MyStreamInfo object out of the pointer?
  Am I doing the rest right?
  username, password, etc are of course members of my class that I set elsewhere.</p>