The Data Model
数据模型
Your app’s data model comprises your data structures and the business logic needed to keep that data in a consistent state. You never want to design your data model in total isolation from your app’s user interface; however, the implementation of your data model objects should be separate and not rely on the presence of specific views or view controllers. Keeping your data separate from your user interface makes it easier to implement a universal app—one that can run on both iPad and iPhone—and also makes it easier to reuse portions of your code later.
数据模型包括数据结构和在一致状态下保持数据的业务逻辑。你不会想要在与用户界面完全隔离的情况下设计数据模型。但是,数据模型对象的实现应该分离并且不依赖于具体视图的显示或者视图控制器。保持你的数据与用户界面分离会使得实现一个通用app更简单些,也使得稍后你重用代码更加方便。
If you have not yet defined your data model, the iOS frameworks provide help for doing so. The following sections highlight some of the technologies you can use when defining specific types of data models.
如果你还没有定义你的数据模型,iOS框架提供了帮助。下面的章节提到的技术你可以在定义具体的数据模型类型的时候使用。
Defining a Custom Data Model
定义一个用户数据模型
When defining a custom data model, create custom objects to represent any high-level constructs but take advantage of the system-supplied objects for simpler data types. The Foundation framework provides many objects (most of which are listed in Table 2-2) for managing strings, numbers, and other types of simple data in an object-oriented way. Using these objects is preferable to defining new objects both because it saves time and because many other system routines expect you to use the built-in objects anyway.
当定义一个用户数据模型的时候,创建用户对象来负责任何高层的结构,但是系统提供的对象对于简单的数据类型更占有优势。基础框架提供了许多对象(大多数列在表2-2中),关于管理字符串,数字和其他类型的简单数据,用面向对象的方法。用这些对象定义新的对象也是可取的,因为它节省了时间,并且因为其他的系统运行时希望你使用内建的对象。
表 2-2 基础框架中的数据类型
Stings and text:
NSSting(NSMuteableString)
NSAttributedSting
(NSMutableAttibutedString)
Strings in iOS are Unicode based. The string classes provide support for creating and manipulating strings in a variety of ways. The attributed string classes support stylized text and are used only in conjunction with Core Text.
iOS中的字符串是基于Unicode的。字符串类提供了用于创建和操作多种方式的字符串的支持。
Numbers:
NSNumber
NSDecimalNumber
NSIndexPath
When you want to store numerical values in a collection, use number objects. The NSNumber class can represent integer, floating-point values, Booleans, and char types. The NSIndexPath class stores a sequence of numbers and is often used to specify multi-layer selections in hierarchical lists.
当你想在一个collection中存储一些数值变量的时候,使用number对象。NSNumber类能够表示整数,浮点,布尔和字符类型。NSIndexPath类存储一个数组序列并且经常用于指定层次结构列出的多种选择。
Raw bytes:
NSData(NSMutableData)
NSValue
For times when you need to store raw streams of bytes, use data objects. Data objects are also commonly used to store objects in an archived form. The NSValue class is typically extended (using categories) and used to archive common data types such as points and rectangles.
当你想要存储一些原始字节流数据的时候,使用data对象。Data对象也经常用于存储归档形式的对象。NSValue类是典型的扩展(使用类别)并且用来归档常用数据类型,比如点和矩形。
Dates and times:
NSDate
NSDateComponents
Use date objects to store timestamps, calendar dates, and other time-related information.
使用data对象来存储时间戳,日历数据和其他的时间相关的信息。
URLs:
NSURL
In addition to their traditional use for referring to network resources, URLs in iOS are the preferred way to store paths to files. The NSURL class even provides support for getting and setting file-related attributes.
除了对网络资源的传统使用,iOS中的URLs是文件存储路径的首选。NSURL类甚至对获取和设置文件相关的属性提供了支持。
Collections:
NSArray(NSMutableArray)
NSDictionary
(NSMutableDictionary)
NSIndexSet
(NSMutableIndexSet)
NSOrderedSet
(NSMutableOrderedSet)
NSSet(NSMutablesSet)
Use collections to group related objects together in a single place. The Foundation framework provides several different types of collection classes
使用collections在一个单独的地方来组织相关的对象。基础框架提供了一些不同的collection类的类型。
In addition to data-related objects, there are some other data types that are commonly used by the iOS frameworks to manage familiar types of data. You are encouraged to use these data types in your own custom objects to represent similar types of data.
除了数据相关的对象,还有一些其他的数据类型也经常由iOS框架使用来管理相似的数据类型。鼓励在你自己的对象中使用这些类型来表达相似的数据类型。
NSInteger/NSUInteger—Abstractions for scalar signed and unsigned integers that define the integer size based on the architecture.
NSInteger/NSUInteger——基于体系结构定义为整型大小,有符号和无符号的整数。
NSRange—A structure used to define a contiguous portion of a series. For example, you can use ranges to define the selected characters in a string.
NSRangs——一个结构,用来定义一系列的连续的部分。比如你可以使用ranges来定义一个字符串中选中的字符。
NSTimeInterval—The number of seconds (whole and partial) in a given time interval.
NSTimeInterVal——秒的数值(整体和部分)在一个给出的time interval中。
CGPoint—An x and y coordinate value that defines a location.
CGPoint——定义一个位置用的x,y坐标值。
CGSize—Coordinate values that define a set of horizontal and vertical extents.
CGSize——定义长宽集合的坐标值
CGRect—Coordinate values that define a rectangular region.
CGRect——定义矩形区域的坐标值
Of course, when defining custom objects, you can always incorporate scalar values directly into your class implementations. In fact, a custom data object can include a mixture of scalar and object types for its member variables. Listing 2-1 shows a sample class definition for a collection of pictures. The class in this instance contains an array of images and a list of the indexes into that array representing the selected items. The class also contains a string for the collection’s title and a scalar Boolean variable indicating whether the collection is currently editable.
当然了,当定义用户对象的时候,你总可以直接融入标量值到你的类实现中。实际上一个用户数据对象能够包括一个标量和成员值的对象类型的混合。列表2-1显示了一个图像的collection的简单的类定义。实例中的类包含了一个images数组和一个数组的索引列表,表示选择的条目。类也包含了一个collection的标题的字符串和一个标量布尔变量,说明当前的collection是否可以编辑。
Listing 2-1 Definition of a custom data object
@interface PictureCollection : NSObject {
NSMutableOrderedSet* pictures;
NSMutableIndexSet* selection;
NSString* title;
BOOL editable;
}
@property (nonatomic, strong) NSString * title;
@property (nonatomic, readonly) NSOrderedSet* pictures;
// Method definitions...
@end
Note: When defining data objects, it is strongly recommended that you declare properties for any member variables that you to expose to clients of the object. Synthesizing these properties in your implementation file automatically creates appropriate accessor methods with the attributes you require. This ensures that object relationships are maintained appropriately and that objects are not released.
注意:当定义数据对象的时候,它强烈要求你为每一个你暴露给对象的客户的成员变量声明属性。在你的实现文件里综合这些属性自动的为你请求的属性创建属性访问方法。这确保了对象关系被恰当的维护并且对象没有被释放。
Consider how undo operations on your custom objects might be handled. Supporting undo means being able to reverse changes made to your objects cleanly. If your objects incorporate complex business logic, you need to factor that logic in a way that can be undone easily. Here are some tips for implementing undo support in your custom objects:
考虑到在你的用户对象上如何取消操作可能会被处理。支持undo意味着能够进行相反的改变,使得用户对象清理干净。如果你的对象有复杂的业务逻辑,你需要让逻辑有一种可以简单undo的方法。这里对于让你的用户对象支持undo操作有一些建议:
Define the methods you need to make sure that changes to your object are symmetrical. For example, if you define a method to add an item, make sure you have a method for removing an item in a similar way.
确保你定义的改变对象的方法是对称的。比如,你定义了一个方法来增加一个条目,那么你要保证也存在一个方法是移除一个条目。
Factor out your business logic from the code you use to change the values of member variables.
从代码中分解出改变成员变量值的业务逻辑。
For multistep actions, use the current NSUndoManager object to group the steps together.
对于多步骤的动作,使用当前的NSUndoManager来把这些步骤组织在一起。
For more information about how to implement undo support in your app, see Undo Architecture. For more information about the classes of the Foundation framework, see Foundation Framework Reference.
关于如何在app中实现undo支持参考"Undo Architecture",关于基础框架类的更多信息参考Foundation Framework Reference.
Defining a Structured Data Model Using Core Data
使用Core Data定义一个结构化的数据模型
Core Data is a schema-driven object graph management and persistence framework. Fundamentally, Core Data helps you to save model objects (in the sense of the model-view-controller design pattern) to a file and get them back again. This is similar to archiving (see Archives and Serializations Programming Guide), but Core Data offers much more than that.
Core Data是一个架构驱动对象图形管理和持久框架。基础的说,Core Data帮助你保存模型对象(类似MCV设计模式)为一个文件,并且取回它们。这类似于归档(参考Archives and Serializations Programming Guide),但是Core Data比那提供了更多。
Core Data provides an infrastructure for managing all the changes to your model objects. This gives you automatic support for undo and redo, and for maintaining reciprocal relationships between objects.
Core Data提供了管理你的模型对象所有改变的基础设置。这对undo和redo是自动支持的,并且维护了对象间的相互关系。
It allows you to keep just a subset of your model objects in memory at any given time, which is very important for iOS apps.
在给定的任何时间内在内存中允许你保存你的模型对象的一个子集,这在iOS apps中非常重要。
It uses a schema to describe the model objects. You define the principal features of your model classes—including the relationships between them—in a GUI-based editor. This provides a wealth of basic functionality “for free,” including setting of default values and attribute value validation.
它使用一个架构来描述模型对象。你定义模型类的主要特性,包括在基于图形界面的编辑器之间的关系。这提供了一个“自由”的丰富的基本功能,包括设置默认。
It allows you to maintain disjoint sets of edits of your objects. This is useful if you want to, for example, allow the user to make edits in one view that may be discarded without affecting data displayed in another view.
它允许你维护不相交的集合的编辑对象。当你需要的时候,这点很有用,举例,允许用户编辑视图,而这个视图可能会被丢弃,而不会影响到另一个视图中显示的数据。
It has an infrastructure for data store versioning and migration. This lets you easily upgrade an old version of the user’s file to the current version.
它为数据的存储版本和迁移提供了基础设施。这会让你把用户文件从旧版本升级到新版本变的很方便。
It allows you to store your data in iCloud and access it from multiple devices.
For information about how to use Core Data, see Core Data Programming Guide.
它允许你在iCloud中存储数据并从多个设备访问。关于如何使用Core Data的信息参考Core Data Programming Guide
Defining a Document-Based Data Model
定义一个基于文档的数据模型
A document-based data model is a convenient way to manage the files your app writes to disk. In this type of data model, you use a document object to represent the contents of a single file (or file package) on disk. That document object is responsible for reading and writing the contents of the file and working with your app’s view controllers to present the document’s contents on screen. The traditional use for document objects is to manage files containing user data. For example, an app that creates and manages text files would use a separate document object to manage each text file. However, you can use document objects for private app data that is also backed by a file.
一个基于文档的数据模型是管理app文件写到磁盘上的方便的方法。这种类型的数据模型,使用一个文档对象来表示磁盘上单个文件的内容(或文件包)。这个文档对象负责读写文件内容,与视图配合工作来在屏幕上显示文档内容。通常使用文档对象来管理包含用户数据的文件。举例,一个创建和管理文本文件的app应该使用独立的文档对象来管理每一个文本文件。但是,你可以为私有app数据使用文本对象,也由一个文件备份。
Figure 2-2 illustrates the typical relationships between documents, files, and the objects in your app’s data model. With few exceptions, each document is self-contained and does not interact directly with other documents. The document manages a single file (or file package) and creates the in-memory representation of any data found in that file. Because the contents of each file are unique, the data structures associated with each document are also unique.
图2-2说明了文档,文件和应用程序数据模型对象之间的典型关系。有一点例外的是,每一个文档都是自包含的,不直接与其他文档交互。文档管理一个单独的文件或文件包,并且为在文件中找到的任何数据创建在内存的表达。因为每个文件的内容是独一无二的,每个文档相关的数据结构也是第一无二的。
图2-2 使用文档来管理文件内容
Application data model
Document Document Document
^ ^ ^
| | |
V V V
File File File
File System
You use the UIDocument class to implement document objects in your iOS app. This class provides the basic infrastructure needed to handle the file management aspects of the document. Other benefits of UIDocument include:
在iOS app中使用UIDocument类来实现文档对象。这个类提供了处理文件管理的基本功能。UIDocument的其他优点有:
It provides support for autosaving the document contents at appropriate times.
它提供了在合适的时间自动保存文档内容
It handles the required file coordination for documents stored in iCloud. It also provides hooks for resolving version conflicts.
它处理了在iCloud中存储文档的和要存储的文件的协调工作。它也提供了解决版本冲突的钩子。
It provides support for undoing actions.
它提供了undo操作的支持。
You must subclass UIDocument in order to implement the specific behavior required by your app’s documents. For detailed information about how to implement a document-based app using UIDocument, see Document-Based Application Programming Guide for iOS.
为了实现应用程序的文档所要求的具体的行为你必须子类化UIDocument。关于如何使用UIDocment实现一个基于文档的应用程序,参考"Document-Based Application Programming Guilide for iOS"
数据模型
Your app’s data model comprises your data structures and the business logic needed to keep that data in a consistent state. You never want to design your data model in total isolation from your app’s user interface; however, the implementation of your data model objects should be separate and not rely on the presence of specific views or view controllers. Keeping your data separate from your user interface makes it easier to implement a universal app—one that can run on both iPad and iPhone—and also makes it easier to reuse portions of your code later.
数据模型包括数据结构和在一致状态下保持数据的业务逻辑。你不会想要在与用户界面完全隔离的情况下设计数据模型。但是,数据模型对象的实现应该分离并且不依赖于具体视图的显示或者视图控制器。保持你的数据与用户界面分离会使得实现一个通用app更简单些,也使得稍后你重用代码更加方便。
If you have not yet defined your data model, the iOS frameworks provide help for doing so. The following sections highlight some of the technologies you can use when defining specific types of data models.
如果你还没有定义你的数据模型,iOS框架提供了帮助。下面的章节提到的技术你可以在定义具体的数据模型类型的时候使用。
Defining a Custom Data Model
定义一个用户数据模型
When defining a custom data model, create custom objects to represent any high-level constructs but take advantage of the system-supplied objects for simpler data types. The Foundation framework provides many objects (most of which are listed in Table 2-2) for managing strings, numbers, and other types of simple data in an object-oriented way. Using these objects is preferable to defining new objects both because it saves time and because many other system routines expect you to use the built-in objects anyway.
当定义一个用户数据模型的时候,创建用户对象来负责任何高层的结构,但是系统提供的对象对于简单的数据类型更占有优势。基础框架提供了许多对象(大多数列在表2-2中),关于管理字符串,数字和其他类型的简单数据,用面向对象的方法。用这些对象定义新的对象也是可取的,因为它节省了时间,并且因为其他的系统运行时希望你使用内建的对象。
表 2-2 基础框架中的数据类型
Stings and text:
NSSting(NSMuteableString)
NSAttributedSting
(NSMutableAttibutedString)
Strings in iOS are Unicode based. The string classes provide support for creating and manipulating strings in a variety of ways. The attributed string classes support stylized text and are used only in conjunction with Core Text.
iOS中的字符串是基于Unicode的。字符串类提供了用于创建和操作多种方式的字符串的支持。
Numbers:
NSNumber
NSDecimalNumber
NSIndexPath
When you want to store numerical values in a collection, use number objects. The NSNumber class can represent integer, floating-point values, Booleans, and char types. The NSIndexPath class stores a sequence of numbers and is often used to specify multi-layer selections in hierarchical lists.
当你想在一个collection中存储一些数值变量的时候,使用number对象。NSNumber类能够表示整数,浮点,布尔和字符类型。NSIndexPath类存储一个数组序列并且经常用于指定层次结构列出的多种选择。
Raw bytes:
NSData(NSMutableData)
NSValue
For times when you need to store raw streams of bytes, use data objects. Data objects are also commonly used to store objects in an archived form. The NSValue class is typically extended (using categories) and used to archive common data types such as points and rectangles.
当你想要存储一些原始字节流数据的时候,使用data对象。Data对象也经常用于存储归档形式的对象。NSValue类是典型的扩展(使用类别)并且用来归档常用数据类型,比如点和矩形。
Dates and times:
NSDate
NSDateComponents
Use date objects to store timestamps, calendar dates, and other time-related information.
使用data对象来存储时间戳,日历数据和其他的时间相关的信息。
URLs:
NSURL
In addition to their traditional use for referring to network resources, URLs in iOS are the preferred way to store paths to files. The NSURL class even provides support for getting and setting file-related attributes.
除了对网络资源的传统使用,iOS中的URLs是文件存储路径的首选。NSURL类甚至对获取和设置文件相关的属性提供了支持。
Collections:
NSArray(NSMutableArray)
NSDictionary
(NSMutableDictionary)
NSIndexSet
(NSMutableIndexSet)
NSOrderedSet
(NSMutableOrderedSet)
NSSet(NSMutablesSet)
Use collections to group related objects together in a single place. The Foundation framework provides several different types of collection classes
使用collections在一个单独的地方来组织相关的对象。基础框架提供了一些不同的collection类的类型。
In addition to data-related objects, there are some other data types that are commonly used by the iOS frameworks to manage familiar types of data. You are encouraged to use these data types in your own custom objects to represent similar types of data.
除了数据相关的对象,还有一些其他的数据类型也经常由iOS框架使用来管理相似的数据类型。鼓励在你自己的对象中使用这些类型来表达相似的数据类型。
NSInteger/NSUInteger—Abstractions for scalar signed and unsigned integers that define the integer size based on the architecture.
NSInteger/NSUInteger——基于体系结构定义为整型大小,有符号和无符号的整数。
NSRange—A structure used to define a contiguous portion of a series. For example, you can use ranges to define the selected characters in a string.
NSRangs——一个结构,用来定义一系列的连续的部分。比如你可以使用ranges来定义一个字符串中选中的字符。
NSTimeInterval—The number of seconds (whole and partial) in a given time interval.
NSTimeInterVal——秒的数值(整体和部分)在一个给出的time interval中。
CGPoint—An x and y coordinate value that defines a location.
CGPoint——定义一个位置用的x,y坐标值。
CGSize—Coordinate values that define a set of horizontal and vertical extents.
CGSize——定义长宽集合的坐标值
CGRect—Coordinate values that define a rectangular region.
CGRect——定义矩形区域的坐标值
Of course, when defining custom objects, you can always incorporate scalar values directly into your class implementations. In fact, a custom data object can include a mixture of scalar and object types for its member variables. Listing 2-1 shows a sample class definition for a collection of pictures. The class in this instance contains an array of images and a list of the indexes into that array representing the selected items. The class also contains a string for the collection’s title and a scalar Boolean variable indicating whether the collection is currently editable.
当然了,当定义用户对象的时候,你总可以直接融入标量值到你的类实现中。实际上一个用户数据对象能够包括一个标量和成员值的对象类型的混合。列表2-1显示了一个图像的collection的简单的类定义。实例中的类包含了一个images数组和一个数组的索引列表,表示选择的条目。类也包含了一个collection的标题的字符串和一个标量布尔变量,说明当前的collection是否可以编辑。
Listing 2-1 Definition of a custom data object
@interface PictureCollection : NSObject {
NSMutableOrderedSet* pictures;
NSMutableIndexSet* selection;
NSString* title;
BOOL editable;
}
@property (nonatomic, strong) NSString * title;
@property (nonatomic, readonly) NSOrderedSet* pictures;
// Method definitions...
@end
Note: When defining data objects, it is strongly recommended that you declare properties for any member variables that you to expose to clients of the object. Synthesizing these properties in your implementation file automatically creates appropriate accessor methods with the attributes you require. This ensures that object relationships are maintained appropriately and that objects are not released.
注意:当定义数据对象的时候,它强烈要求你为每一个你暴露给对象的客户的成员变量声明属性。在你的实现文件里综合这些属性自动的为你请求的属性创建属性访问方法。这确保了对象关系被恰当的维护并且对象没有被释放。
Consider how undo operations on your custom objects might be handled. Supporting undo means being able to reverse changes made to your objects cleanly. If your objects incorporate complex business logic, you need to factor that logic in a way that can be undone easily. Here are some tips for implementing undo support in your custom objects:
考虑到在你的用户对象上如何取消操作可能会被处理。支持undo意味着能够进行相反的改变,使得用户对象清理干净。如果你的对象有复杂的业务逻辑,你需要让逻辑有一种可以简单undo的方法。这里对于让你的用户对象支持undo操作有一些建议:
Define the methods you need to make sure that changes to your object are symmetrical. For example, if you define a method to add an item, make sure you have a method for removing an item in a similar way.
确保你定义的改变对象的方法是对称的。比如,你定义了一个方法来增加一个条目,那么你要保证也存在一个方法是移除一个条目。
Factor out your business logic from the code you use to change the values of member variables.
从代码中分解出改变成员变量值的业务逻辑。
For multistep actions, use the current NSUndoManager object to group the steps together.
对于多步骤的动作,使用当前的NSUndoManager来把这些步骤组织在一起。
For more information about how to implement undo support in your app, see Undo Architecture. For more information about the classes of the Foundation framework, see Foundation Framework Reference.
关于如何在app中实现undo支持参考"Undo Architecture",关于基础框架类的更多信息参考Foundation Framework Reference.
Defining a Structured Data Model Using Core Data
使用Core Data定义一个结构化的数据模型
Core Data is a schema-driven object graph management and persistence framework. Fundamentally, Core Data helps you to save model objects (in the sense of the model-view-controller design pattern) to a file and get them back again. This is similar to archiving (see Archives and Serializations Programming Guide), but Core Data offers much more than that.
Core Data是一个架构驱动对象图形管理和持久框架。基础的说,Core Data帮助你保存模型对象(类似MCV设计模式)为一个文件,并且取回它们。这类似于归档(参考Archives and Serializations Programming Guide),但是Core Data比那提供了更多。
Core Data provides an infrastructure for managing all the changes to your model objects. This gives you automatic support for undo and redo, and for maintaining reciprocal relationships between objects.
Core Data提供了管理你的模型对象所有改变的基础设置。这对undo和redo是自动支持的,并且维护了对象间的相互关系。
It allows you to keep just a subset of your model objects in memory at any given time, which is very important for iOS apps.
在给定的任何时间内在内存中允许你保存你的模型对象的一个子集,这在iOS apps中非常重要。
It uses a schema to describe the model objects. You define the principal features of your model classes—including the relationships between them—in a GUI-based editor. This provides a wealth of basic functionality “for free,” including setting of default values and attribute value validation.
它使用一个架构来描述模型对象。你定义模型类的主要特性,包括在基于图形界面的编辑器之间的关系。这提供了一个“自由”的丰富的基本功能,包括设置默认。
It allows you to maintain disjoint sets of edits of your objects. This is useful if you want to, for example, allow the user to make edits in one view that may be discarded without affecting data displayed in another view.
它允许你维护不相交的集合的编辑对象。当你需要的时候,这点很有用,举例,允许用户编辑视图,而这个视图可能会被丢弃,而不会影响到另一个视图中显示的数据。
It has an infrastructure for data store versioning and migration. This lets you easily upgrade an old version of the user’s file to the current version.
它为数据的存储版本和迁移提供了基础设施。这会让你把用户文件从旧版本升级到新版本变的很方便。
It allows you to store your data in iCloud and access it from multiple devices.
For information about how to use Core Data, see Core Data Programming Guide.
它允许你在iCloud中存储数据并从多个设备访问。关于如何使用Core Data的信息参考Core Data Programming Guide
Defining a Document-Based Data Model
定义一个基于文档的数据模型
A document-based data model is a convenient way to manage the files your app writes to disk. In this type of data model, you use a document object to represent the contents of a single file (or file package) on disk. That document object is responsible for reading and writing the contents of the file and working with your app’s view controllers to present the document’s contents on screen. The traditional use for document objects is to manage files containing user data. For example, an app that creates and manages text files would use a separate document object to manage each text file. However, you can use document objects for private app data that is also backed by a file.
一个基于文档的数据模型是管理app文件写到磁盘上的方便的方法。这种类型的数据模型,使用一个文档对象来表示磁盘上单个文件的内容(或文件包)。这个文档对象负责读写文件内容,与视图配合工作来在屏幕上显示文档内容。通常使用文档对象来管理包含用户数据的文件。举例,一个创建和管理文本文件的app应该使用独立的文档对象来管理每一个文本文件。但是,你可以为私有app数据使用文本对象,也由一个文件备份。
Figure 2-2 illustrates the typical relationships between documents, files, and the objects in your app’s data model. With few exceptions, each document is self-contained and does not interact directly with other documents. The document manages a single file (or file package) and creates the in-memory representation of any data found in that file. Because the contents of each file are unique, the data structures associated with each document are also unique.
图2-2说明了文档,文件和应用程序数据模型对象之间的典型关系。有一点例外的是,每一个文档都是自包含的,不直接与其他文档交互。文档管理一个单独的文件或文件包,并且为在文件中找到的任何数据创建在内存的表达。因为每个文件的内容是独一无二的,每个文档相关的数据结构也是第一无二的。
图2-2 使用文档来管理文件内容
Application data model
Document Document Document
^ ^ ^
| | |
V V V
File File File
File System
You use the UIDocument class to implement document objects in your iOS app. This class provides the basic infrastructure needed to handle the file management aspects of the document. Other benefits of UIDocument include:
在iOS app中使用UIDocument类来实现文档对象。这个类提供了处理文件管理的基本功能。UIDocument的其他优点有:
It provides support for autosaving the document contents at appropriate times.
它提供了在合适的时间自动保存文档内容
It handles the required file coordination for documents stored in iCloud. It also provides hooks for resolving version conflicts.
它处理了在iCloud中存储文档的和要存储的文件的协调工作。它也提供了解决版本冲突的钩子。
It provides support for undoing actions.
它提供了undo操作的支持。
You must subclass UIDocument in order to implement the specific behavior required by your app’s documents. For detailed information about how to implement a document-based app using UIDocument, see Document-Based Application Programming Guide for iOS.
为了实现应用程序的文档所要求的具体的行为你必须子类化UIDocument。关于如何使用UIDocment实现一个基于文档的应用程序,参考"Document-Based Application Programming Guilide for iOS"