SwiftyUTType 1.0.3

SwiftyUTType 1.0.3

NoodleOfDeath 维护。



  • NoodleOfDeath

SwiftyUTType

CI Status Version License Platform

Apple 的 MobileCoreServices 框架为开发者提供了大量的内置 统一类型标识符,可以通过关联文件与反向 DNS 标识符来为文件扩展名和开发环境之间添加上下文意义。这些对用户来说是内置的字符串常量,不容易枚举或被开发者知晓。通过定义 UTType 结构,开发者现在可以动态引用并扩展这些内置的统一类型标识符,并通过面向对象设计方便地检查统一类型的一致性。

示例

要运行示例项目,请克隆仓库,然后先从 Example 目录运行 pod install

需求

安装

SwiftyUTType 可通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile 中

pod 'SwiftyUTType'

使用

StringURL 生成统一类型标识符

import SwiftyUTType

print("my-file.css".uttype) // prints "public.css"
print("my-file.html".uttype) // prints "public.html"
print("my-file.java".uttype) // prints "com.sun.java-source"
print("my-file.jpg".uttype) // prints "public.jpeg"
print("my-file.js".uttype) // prints "com.netscpe.javascript-source"
print("my-file.json".uttype) // prints "public.json"
print("my-file.md".uttype) // prints "dyn."; custom document type like this need to be specified in the Info.plist
print("my-file.mp4".uttype) // prints "public.mpeg-4"
print("my-file.plist".uttype) // prints "com.apple.property-list"
print("my-file.png".uttype) // prints "public.png"
print("my-file.swift".uttype) // prints "public.swift-source"
print("my-file.tiff".uttype) // prints "public.tiff"
print("my-file.txt".uttype) // prints "public.plain-text"

print(Bundle.main.url(forResource: "Info" , withExtension: "plist")!.uttype) // prints "com.apple.property-list"

测试统一类型兼容性

import SwiftyUTType

print("my-file.jpg".uttype.conforms(to: .Image)) // prints "true"
print("my-file.jpg".uttype.conforms(to: .JPEG)) // prints "true"
print("my-file.jpg".uttype.conforms(to: .PNG)) // prints "false"
print("my-file.jpg".uttype.conforms(to: .PNG, .TIFF, .JPEG))  // prints "true"

print("my-file.png".uttype.conforms(to: .Image)) // prints "true"
print("my-file.png".uttype.conforms(to: .JPEG)) // prints "false"
print("my-file.png".uttype.conforms(to: .PNG))  // prints "true"
print("my-file.png".uttype.conforms(to: .PNG, .TIFF, .JPEG))  // prints "true"

print("my-file.mp4".uttype.conforms(to: .Video, .Movie)) // prints "true""

print("my-file.jpg".uttype.conforms(to: .Image, .JPEG, mustConformToAll: true))  // prints "true"
print("my-file.jpg".uttype.conforms(to: .Image, .PNG, mustConformToAll: true)) // prints "false"

print("my-file.png".uttype.conforms(to: .Image, .JPEG, mustConformToAll: true))  // prints "false"
print("my-file.png".uttype.conforms(to: .Image, .PNG, mustConformToAll: true)) // prints "true"

print("my-file.jpg".uttype.conforms(toFirst: .TIFF, .JPEG)!) // prints "public.jpeg"
print("my-file.jpg".uttype.conforms(toFirst: .TIFF, .Image, .JPEG)!) // prints "public.image"

print("my-file.png".uttype.conforms(toFirst: .TIFF, .JPEG))  // prints "nil"
print("my-file.png".uttype.conforms(toFirst: .TIFF, .Image, .JPEG)!) // prints "public.image"

作者

NoodleOfDeath, [email protected]

许可

SwiftyUTType 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。