美文网首页
Resource Programming Guide.

Resource Programming Guide.

作者: ngugg | 来源:发表于2018-10-29 14:07 被阅读1次

    相关链接: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/LoadingResources/Introduction/Introduction.html#//apple_ref/doc/uid/10000051i

    Applied to computer programs, resources are data files that accompany a program’s executable code. Resources simplify the code you have to write by moving the creation of complex sets of data or graphical content outside of your code and into more appropriate tools. For example, rather than creating images pixel by pixel using code, it is much more efficient (and practical) to create them in an image editor. To take advantage of a resource, all your code has to do is load it at runtime and use it.

    • 应用于计算机程序,资源是伴随程序可执行代码的数据文件。 资源通过在代码之外移动复杂数据集或图形内容的创建以及更合适的工具来简化您必须编写的代码。 例如,不是使用代码逐个像素地创建图像,而是在图像编辑器中创建它们更有效(和实用)。 要利用资源,您的所有代码都必须在运行时加载它并使用它。

    In addition to simplifying your code, resources are also an intimate part of the internationalization process for all applications. Rather than hard-coding strings and other user-visible content in your application, you can place that content in external resource files. Localizing your application then becomes a simple process of creating new versions of each resource file for each supported language. The bundle mechanism used in both OS X and iOS provides a way to organize localized resources and to facilitate the loading of resource files that match the user’s preferred language.

    • 除了简化代码之外,资源也是所有应用程序国际化过程的一个紧密部分。 您可以将该内容放在外部资源文件中,而不是在应用程序中对字符串和其他用户可见内容进行硬编码。 然后,本地化您的应用程序将成为为每种受支持的语言创建每个资源文件的新版本的简单过程。 OS X和iOS中使用的捆绑机制提供了一种组织本地化资源并便于加载与用户首选语言匹配的资源文件的方法。

    This document provides information about the types of resources supported in OS X and iOS and how you use those resources in your code. This document does not focus on the resource-creation process. Most resources are created using either third-party applications or the developer tools provided in the /Developer/Applications directory. In addition, although this document refers to the use of resources in applications, the information also applies to other types of bundled executables, including frameworks and plug-ins.

    • 本文档提供有关OS X和iOS中支持的资源类型以及如何在代码中使用这些资源的信息。 本文档不关注资源创建过程。 大多数资源是使用第三方应用程序或/ Developer / Applications目录中提供的开发人员工具创建的。 此外,尽管本文档涉及在应用程序中使用资源,但该信息也适用于其他类型的捆绑可执行文件,包括框架和插件。

    Before reading this document, you should be familiar with the organizational structure imposed by application bundles. Understanding this structure makes it easier to organize and find the resource files your application uses. For information on the structure of bundles, see Bundle Programming Guide.

    • 在阅读本文档之前,您应该熟悉应用程序包强加的组织结构。 了解此结构可以更轻松地组织和查找应用程序使用的资源文件。 有关bundle的结构的信息,请参阅Bundle Programming Guide。

    At a Glance

    Applications can contain many types of resources but there are several that are supported directly by iOS and OS X.

    • 应用程序可以包含许多类型的资源,但iOS和OS X直接支持多种资源。

    Nib Files Store the Objects of Your Application’s User Interface

    • Nib文件存储应用程序用户界面的对象

    Nib files are the quintessential resource type used to create iOS and Mac apps. A nib file is a data archive that essentially contains a set of freeze-dried objects that you want to recreate at runtime. Nib files are used most commonly to store preconfigured windows, views, and other visually oriented objects but they can also store nonvisual objects such as controllers.

    • Nib文件是用于创建iOS和Mac应用程序的典型资源类型。 nib文件是一个数据存档,它基本上包含一组您希望在运行时重新创建的冻干对象。 Nib文件最常用于存储预配置的窗口,视图和其他可视化对象,但它们也可以存储非可视对象,如控制器。

    You edit nib files in Xcode with Interface Builder, which provides a graphical editor for assembling your objects. When you subsequently load a nib file into your application, the nib-loading code instantiates each object in the file and restores it to the state you specified in Interface Builder. Thus, what you see in Interface Builder is really what you get in your application at runtime.

    • 您可以使用Interface Builder编辑Xcode中的nib文件,Interface Builder提供了一个用于组装对象的图形编辑器。 当您随后将nib文件加载到应用程序中时,nib加载代码会实例化文件中的每个对象,并将其还原到您在Interface Builder中指定的状态。 因此,您在Interface Builder中看到的就是您在运行时在应用程序中获得的内容。
      Relevant Chapters: Nib Files
    String Resources Containing Localizable Text

    Text is a prominent part of most user interfaces but also a resource that is most affected by localization changes. Rather than hard-coding text into your code, iOS and OS X support the storage of user-visible text in strings files, which are human-readable text files (in the UTF-16 encoding) containing a set of string resources for an application. (The use of the plural “strings” in is deliberate and due to the .strings filename extension used by files of that type.) Strings files greatly simplify the internationalization and localization process by allowing you to write your code once and then load the appropriately localized text from resource files that can be changed easily.

    • 文本是大多数用户界面的重要部分,但也是受本地化更改影响最大的资源。 iOS和OS X不支持将文本硬编码到代码中,而是支持在字符串文件中存储用户可见文本,字符串文件是人类可读的文本文件(UTF-16编码),包含应用程序的一组字符串资源。 (使用多个“字符串”是故意的,并且由于该类型文件使用的.strings文件扩展名。)字符串文件允许您编写一次代码然后适当加载,大大简化了国际化和本地化过程。 资源文件中的本地化文本,可以轻松更改。

    The Core Foundation and Foundation frameworks provide the facilities for loading text from strings files. Applications that use these facilities can also take advantage of tools that come with Xcode to generate and maintain these resource files throughout the development process.

    • Core Foundation和Foundation框架提供了从字符串文件加载文本的工具。 使用这些工具的应用程序还可以利用Xcode附带的工具在整个开发过程中生成和维护这些资源文件。

    Relevant Chapters: String Resources

    Images, Sounds, and Movies Represent Pre-rendered Content
    Images, sound, and movie resources play an important role in iOS and Mac apps. Images are responsible for creating the unique visual style used by each operating system; they also help simplify your drawing code for complex visual elements. Sound and movie files similarly help enhance the overall user experience of your application while simplifying the code needed to create that experience. Both operating systems provide extensive support for loading and presenting these types of resources in your applications.

    • 图像,声音和电影代表预渲染内容
      图像,声音和电影资源在iOS和Mac应用程序中发挥着重要作用。 图像负责创建每个操作系统使用的独特视觉样式; 它们还有助于简化复杂视觉元素的绘图代码。 声音和电影文件同样有助于增强应用程序的整体用户体验,同时简化创建该体验所需的代码。 这两种操作系统都为在应用程序中加载和显示这些类型的资源提供了广泛的支持。

    Relevant Chapters: Image, Sound, and Video Resources

    Property Lists and Data Files Separate Data from Code

    • 属性列表和数据文件从代码中分离数据

    A property list file is a structured file used to store string, number, Boolean, date, and raw data values. Data items in the file are organized using array and dictionary structures with most items associated with a unique key. The system uses property lists to store simple data sets. For example, the Info.plist file found in nearly every application is an example of a property list file. You can also use property list files for simple data storage needs.

    • 属性列表文件是用于存储字符串,数字,布尔值,日期和原始数据值的结构化文件。 文件中的数据项使用数组和字典结构进行组织,其中大多数项与唯一键相关联。 系统使用属性列表来存储简单数据集。 例如,几乎每个应用程序中的Info.plist文件都是属性列表文件的示例。 您还可以使用属性列表文件来满足简单的数据存储需求。

    In addition to property lists, OS X supports some specially structured files for specific uses. For example, AppleScript data and user help are stored using specially formatted data files. You can also create custom data files of your own.

    • 除了属性列表,OS X还支持一些特殊用途的特殊结构化文件。 例如,AppleScript数据和用户帮助使用特殊格式的数据文件存储。 您还可以创建自己的自定义数据文件。

    Relevant Chapters: Data Resource Files

    iOS Supports Device-Specific Resources

    In iOS 4.0 and later, it is possible to mark individual resource files as usable only on a specific type of device. This capability simplifies the code you have to write for Universal applications. Rather than creating separate code paths to load one version of a resource file for iPhone and a different version of the file for iPad, you can let the bundle-loading routines choose the correct file. All you have to do is name your resource files appropriately.

    • 在iOS 4.0及更高版本中,可以将单个资源文件标记为仅在特定类型的设备上可用。 此功能简化了您必须为Universal应用程序编写的代码。 您可以让捆绑加载例程选择正确的文件,而不是创建单独的代码路径来为iPhone加载一个版本的资源文件,为iPad加载不同版本的文件。 您所要做的就是适当地命名您的资源文件。

    To associate a resource file with a particular device, you add a custom modifier string to its filename. The inclusion of this modifier string yields filenames with the following format:

    • 要将资源文件与特定设备相关联,请在其文件名中添加自定义修饰符字符串。 包含此修饰符字符串会产生具有以下格式的文件名:

    <basename><device>.<filename_extension>

    The <basename> string represents the original name of the resource file. It also represents the name you use when accessing the file from your code. Similarly, the <filename_extension> string is the standard filename extension used to identify the type of the file. The <device> string is a case-sensitive string that can be one of the following values:

    • <basename>字符串表示资源文件的原始名称。 它还表示从代码访问文件时使用的名称。 同样,<filename_extension>字符串是用于标识文件类型的标准文件扩展名。 <device>字符串是区分大小写的字符串,可以是以下值之一:

    ~ipad - The resource should be loaded on iPad devices only.

    • 该资源应仅在iPad设备上加载。

    ~iphone - The resource should be loaded on iPhone or iPod touch devices only.

    • 该资源应仅加载到iPhone或iPod touch设备上。

    You can apply device modifiers to any type of resource file. For example, suppose you have an image named MyImage.png. To specify different versions of the image for iPad and iPhone, you would create resource files with the names MyImage~ipad.png and MyImage~iphone.png and include them both in your bundle. To load the image, you would continue to refer to the resource as MyImage.png in your code and let the system choose the appropriate version, as shown here:

    • 您可以将设备修饰符应用于任何类型的资源文件。 例如,假设您有一个名为MyImage.png的图像。 要为iPad和iPhone指定不同版本的图像,您可以使用名称MyImageipad.png和MyImageiphone.png创建资源文件,并将它们包含在捆绑包中。 要加载图像,您将继续在代码中将资源称为MyImage.png,并让系统选择适当的版本,如下所示:
    UIImage* anImage = [UIImage imageNamed:@"MyImage.png"];
    

    On an iPhone or iPod touch device, the system loads the MyImage~iphone.png resource file, while on iPad, it loads the MyImage~ipad.png resource file. If a device-specific version of a resource is not found, the system falls back to looking for a resource with the original filename, which in the preceding example would be an image named MyImage.png.

    • 在iPhone或iPod touch设备上,系统加载MyImageiphone.png资源文件,而在iPad上加载MyImageipad.png资源文件。 如果未找到特定于设备的资源版本,系统将回退到查找具有原始文件名的资源,在前面的示例中,该资源将是名为MyImage.png的图像。

    See Also

    The following Apple Developer documents are conceptually related to Resource Programming Guide:
    以下Apple Developer文档在概念上与“资源编程指南”相关:

    • Bundle Programming Guide describes the bundle structure used by applications to store executable code and resources.

      • “捆绑编程指南”描述了应用程序用于存储可执行代码和资源的捆绑结构。
    • Internationalization and Localization Guide describes the process of preparing an application (and its resources) for translation into other languages.

      • 国际化和本地化指南描述了准备应用程序(及其资源)以便翻译成其他语言的过程。
    • The chapter Build a User Interface in Xcode Overview describes the tool for editing nib file resources.

      • “Xcode概述”中的“构建用户界面”一章介绍了用于编辑nib文件资源的工具。
    • Property List Programming Guide describes the facilities in place for loading property-list resource files into a Cocoa application.

      • “属性列表编程指南”描述了将属性列表资源文件加载到Cocoa应用程序中的适当设施。
    • Property List Programming Topics for Core Foundation describes the facilities in place for loading property-list resource files into a C-based application.

      • Core Foundation的属性列表编程主题描述了将属性列表资源文件加载到基于C的应用程序中的设施。

    相关文章

      网友评论

          本文标题:Resource Programming Guide.

          本文链接:https://www.haomeiwen.com/subject/ljnvtqtx.html