import Foundation

extension Data
{
    func writeToTemporal(withExtension:String?) -> URL?
    {
        var randomName:String = UUID().uuidString
        
        if let withExtension:String = withExtension
        {
            randomName.append(".")
            randomName.append(withExtension)
        }
        
        let directoryUrl:URL = URL(fileURLWithPath:NSTemporaryDirectory())
        let fileUrl:URL = directoryUrl.appendingPathComponent(randomName)
        
        do
        {
            try write(to:fileUrl, options:Data.WritingOptions.atomicWrite)
        }
        catch
        {
            return nil
        }
        
        return fileUrl
    }
}

import Foundation

extension Data
{
    func writeToTemporal() -> URL?
    {
        let randomName:String = UUID().uuidString
        let directoryUrl:URL = URL(fileURLWithPath:NSTemporaryDirectory())
        let fileUrl:URL = directoryUrl.appendingPathComponent(randomName)
        
        do
        {
            try write(to:fileUrl, options:Data.WritingOptions.atomicWrite)
        }
        catch
        {
            return nil
        }
        
        return fileUrl
    }
}

--- 
+++ 
@@ -2,9 +2,16 @@
 
 extension Data
 {
-    func writeToTemporal() -> URL?
+    func writeToTemporal(withExtension:String?) -> URL?
     {
-        let randomName:String = UUID().uuidString
+        var randomName:String = UUID().uuidString
+        
+        if let withExtension:String = withExtension
+        {
+            randomName.append(".")
+            randomName.append(withExtension)
+        }
+        
         let directoryUrl:URL = URL(fileURLWithPath:NSTemporaryDirectory())
         let fileUrl:URL = directoryUrl.appendingPathComponent(randomName)
         
Add extension to temporal file
