[英]How do I figure out the file type (document type / UTI) of a file
我正在開發一個文檔應用程序。 我的文檔其實是一個可以容納其他任意文檔的包。 當我的文檔被加載時,我可以使用 NSFileWrapper API 獲得所有這些文件的句柄。 我想知道這些 NSFileWrapper 對象代表什么樣的文件,所以我知道如何在我的應用程序中顯示它。 它是文本文件、圖像還是二進制文件?
有沒有辦法獲得 NSFileWrapper 的 UTI? 我確實在來自 AppKit 的 NSFileWrapper 上找到了一個“圖標”屬性。 因此,他們必須通過某種方式知道文件是什么才能查找其圖標。
你可以向NSWorkspace
詢問任何文件的 UTI:
import Cocoa
let documents = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,
.UserDomainMask, true)[0] as! String
let documentPath = documents.stringByAppendingPathComponent("test.pages")
// test.pages will need to exist on the filesystem
NSWorkspace.sharedWorkspace().typeOfFile(documentPath, error: nil)
// com.apple.iwork.pages.sffpages
我不確定它有多可靠,或者如果有多個應用程序能夠打開同一個文件會發生什么。
該文檔鼓勵檢查NSError
但沒有說明可能發生什么錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.