macOS 开发 - 还在用 Pboard 类型?苹果让你用 UTType

来源:苹果官方文档

引入

苹果建议过去 pasteboard 类型的形式不应该再用了,要尽可能使用 UTIs。

Pboard 类型会在未来的发布里被弃用。在 macOS 10.6 和以后应该替换所有 pboard 类型的使用未 UTIs,包括在Types for Standard Data (macOS 10.6 and later)中介绍的常量。

概述

Uniform Type Identifiers(即 UTIs)是唯一标识抽象类型的字符串。它们可以被用来描述文件格式或内存中的数据类型,但也可以被用来描述其它种类实体的类型,比如目录、用量或包。

类型声明出现在二进制 property list,告诉系统关于一个类型的一些事情。关于格式的详细信息以及UTI的声明可以在 Uniform Type Identifiers Overview里找到。
声明中包括的几个关键概念:

  • 一致性。一致性关系在类型之间建立了多个继承体系。类型 property 值在运行时可能被继承,根据每个类型的一致性关系。如果一个类型的声明不包括特定类型 property 的值,然后类型的超类型会被用来搜寻值。超类型被深度优先搜索,顺序就是类型声明中给定的顺序。这是唯一的方式,所以声明的超类型顺序是很重要的。
  • 标签。标签是指明类型的定义的字符串,用一些其他类型的认定算法,比如文件名扩展或MIME类型。其他类型认定算法的命名空间被称作它的。uniform type identifiers 通过类识别它们自己,所以有效的标签类设置在未来可以被轻易扩展。
  • 类型声明也可能包含几个其它 property:类型的本地化用户描述,二进制声明里图标资源的名字,参考 URL 指明关于类型本身的技术文档,以及一个版本号,类型升级的时候可以递增。这些 property 全部都是可选的。

特征

创建 uniform type identifiers

func UTTypeCreatePreferredIdentifierForTag(CFString, CFString, CFString?)

为创建特定标签指明的类型创建uniform type identifier。

func UTTypeCreateAllIdentifiersForTag(CFString, CFString, CFString?)

创建由特定标签指明的类型的 uniform type identifiers 数组。

从 uniform type identifiers 获得标签

用一个不同的类型分类方法翻译一个 uniform type identifier 为一列标签。

测试 uniform type identifiers

func UTTypeEqual(CFString, CFString)

返回两个 uniform type identifiers 是否相等。

func UTTypeConformsTo(CFString, CFString)

返回一个 uniform type identifier 是否符合另一个 uniform type identifier。

获得额外类型信息

func UTTypeCopyDescription(CFString)

返回uniform type identifier相关的本地化的、用户可读的类型描述字符串。

func UTTypeCopyDeclaration(CFString)

返回一个 uniform type 的声明。

func UTTypeCopyDeclaringBundleURL(CFString)

返回类型包含声明的二进制文件位置。

OSType Conversion Utilities

func UTCreateStringForOSType(OSType)

Encodes an OSType into a string suitable for use as a tag argument.
func UTGetOSTypeFromString(CFString)
Decodes a tag string into an OSType.

常量

Type Declaration Dictionary Keys

These are keys for values found in a UTI dictionary. For more information about UTI dictionaries, see [Uniform Type Identifiers Overview](Uniform Type Identifiers Overview).

Type Tag Classes
These strings identify other tagging classification schemes that a uniform type identifier can be converted to and from.

UTI Abstract Types
These are abstract uniform type identifiers that most other type identifiers are derived from.

UTI Concrete Types
These are uniform type identifiers for concrete types.

UTI Text Types
These are the uniform type identifiers for text content.

UTI Composite Content Types
Uniform type identifiers for content that includes multiple data types.

UTI Image Content Types
Uniform type identifiers for graphics content.

UTI Audio Visual Content Types
Uniform type identifier for audio and video content.

UTI Directory Types
Uniform type identifiers for different directory types.

UTI Application Types
Uniform type identifiers for application types.

UTI Contact Types
Uniform type identifiers for contact types.

UTI Miscellaneous Types
Uniform type identifiers for types that do not fit in other categories.

你可能感兴趣的:(macOS 开发 - 还在用 Pboard 类型?苹果让你用 UTType)