2017-09-05 84 views
0

我試圖加載存儲在公共數據庫中的PDF文件作爲CKAsset,代碼在去年發佈的現有應用程序中運行良好。不過,我現在得到以下錯誤將CKAsset加載到UIWebView中

無法將類型「的NSData」的價值強制

這裏鍵入「數據」是代碼 -

if docType == "PDF" || docType == "pdf" { 
    if let asset1 = record.object(forKey: "documentFile") as? CKAsset { 
     let doc1Data : NSData? = NSData(contentsOf:asset1.fileURL) 
     let path = (NSTemporaryDirectory() as NSString).appendingPathComponent(filename) 
     let fileURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(filename) 
     do { 
     try doc1Data!.write(to: URL(fileURLWithPath: path), options: .atomic) 
     } catch { 
     print(error) 
     } 
     self.docWebView.load(doc1Data! as Data, mimeType: "application/pdf", textEncodingName: "UTF-8", baseURL: NSURL() as URL) 
     self.filenameURL = [(fileURL)] 
} 

我敢肯定,必須有一個簡單的解釋,但我不能看到問題

在此先感謝。

+0

唉我仍然得到錯誤 - 無法將類型'NSData'的值轉換爲在'co'中鍵入'數據' ercion涉及到'self.docWebView.loadData(doc1Data as Data ?, MIMEType:「application/pdf」,textEncodingName:「UTF-8」,baseURL:NSURL()爲URL)'代碼行 – Bowcaps

回答

0

嘗試改變以下線:

let doc1Data : NSData? = NSData(contentsOf:asset1.fileURL) 

let doc1Data = NSData(contentsOf:asset1.fileURL) 
+0

唉,我仍然得到錯誤 - 無法將類型'NSData'的值轉換爲與'self.docWebView.loadData(doc1Data as Data?,MIMEType:「application/pdf」,textEncodingName:「UTF-8」,baseURL: NSURL()作爲URL)'代碼行 – Bowcaps

0

這樣看來答案很簡單,改變負載數據到的loadRequest並引用文檔的URL已經獲得它從CKAsset

if docType == "PDF" || docType == "pdf" { 
if let asset1 = record.object(forKey: "documentFile") as? CKAsset { 
let doc1Data : NSData! = NSData(contentsOf:asset1.fileURL) 
let path = (NSTemporaryDirectory() as NSString).appendingPathComponent(filename) 
do { 
    try doc1Data!.write(to: URL(fileURLWithPath: path), options: .atomic) 
    let url = URL(fileURLWithPath: path) 
    let urlRequest = URLRequest(url: url) 
    self.docWebView?.loadRequest(urlRequest) 
    } catch { 
    print(error) 
    } 
}