How to find file UTI for file, withouth pathExtension, in a path in Swift

后端 未结 1 955
情歌与酒
情歌与酒 2020-11-28 14:10

I\'ve been trying to convert this code which I had from this example (in Objective-c) with no luck.

    String *path; // contains the file path

    // Get          


        
相关标签:
1条回答
  • 2020-11-28 14:12

    You can use URL method resourceValues(forKeys:) to get the file URL TypeIdentifierKey which returns the file uniform type identifier (UTI). You just need to check if the returned value is "com.apple.m4v-video" for videos, "public.jpeg" or "public.png" for images and so on. You can add a typeIdentifier property extension to URL to return the TypeIdentifierKey string value as follow:

    Xcode 11 • Swift 5.1

    extension URL {
        var typeIdentifier: String? { (try? resourceValues(forKeys: [.typeIdentifierKey]))?.typeIdentifier }
        var localizedName: String? { (try? resourceValues(forKeys: [.localizedNameKey]))?.localizedName }
    }
    
    0 讨论(0)
提交回复
热议问题