iCloud2_Defining Your Document Subclass
定义你的文件子类Defining Your Document Subclass
管理文件最好的方法是在iCloud中用一个基于UIDocument类的客户文件对象。这个类提供了本地和iCloud中管理文件的基本方式。使用这个类,你必须继承这个类并重写其中关于读写应用数据的方法。The best way to manage a file in iCloud is to use a custom document object based on the UIDocument class. This class provides the basic behavior required for managing files both locally and in iCloud. To use the class, you must subclass it and override the methods responsible for reading and writing your app data.
在MVC结构中,文件对象是模型控制器,他们的工作是你应用数据模型的一部分的控制器对象。一个文件对象典型促进应用数据结构和提供相关数据的视图控制器之间的交互。In the Model-View-Controller architecture, document objects are model controllers—that is, their job is to act as a controller object for portions of your app’s data model. A document object typically facilitates interactions between your app’s data structures and the view controllers that present the associated data.
创建你的UIDocument子类Create Your UIDocument Subclass
简单文本编辑应用使用一个客户UIDocument子类来管理个人文本文件的内容。因为这个类不存在模板项目,你必须显示的创建它。The Simple Text Editor app uses a custom UIDocument subclass to manage the contents of individual text files. Because this class does not exist in the template project, you must create it explicitly.
通过STESimpleTextDocument类的数据管理的组成包括一个字符串对象,存储文件相关的文本。你用一个属性声明这个字符串。The data managed by the STESimpleTextDocument class consists of an NSString object, which stores the text associated with the document. You declare this string using a property.
要实现documentText属性,你还需要告诉编译器要同步访问器的方法。在文件的实现文件中提供相关的代码。To complete the implementation of the documentText property, you also need to tell the compiler to synthesize the accessor methods. You provide code to do this in your document’s implementation file.
重写方法,设置文件数据Override the Method to Set the Document Data
STESimpleTextDocument类为它的documentText属性使用一个客户的setter方法。当设置文件文本时,在这个方法添加取消支持。这不仅会给应用支持在文件的文本的取消改变,还提供一些iCloud的便利。特定的,它会引发文件的自动保存机制,引起这些改变被传送到iCloud。The STESimpleTextDocument class uses a custom setter method for its documentText property. The custom setter method adds undo support when setting the document text. Not only does this give the app support for undoing changes on the document’s text, it also provides some benefits related to iCloud. Specifically, it triggers the document’s autosave mechanism, which causes those changes to be sent to iCloud.
在setDocumentText:方法中,你必须保存包含旧文本的字符串,传递到取消管理者,作为操作的一部分。取消管理者维持者旧字符串,直到它不再被需要。In the setDocumentText: method, you must save the string containing the old text and pass it to the undo manager as part of the operation. The undo manager maintains the reference to the old string until it is no longer needed.
实现方法,读写文件数据Implement the Methods to Read and Write the Document Data
一个文件对象写一个文件的内容到磁盘,然后读取内容到后台。几乎所有需要发起读写操作工作是被UIDocument类处理的。但是因为实际的读写数据操作是指定到你的文件类的,你必须写一些客户代码。A document object writes a document’s contents to disk and reads those contents back in. Nearly all of the work needed to initiate read and write operations is handled automatically by the UIDocument class. But because the actual reading and writing of data is specific to your document class, you must write some custom code.
在这个简单文本编辑应用中,文件对象的数据是一个字符串对象,但是UIDocument不允许你把字符串直接写到磁盘。所以取而代之,你必须将表单中的字符串打包成文件对象可以处理的,命名为NSData的对象。你要在你的文件的contentsForType:error:方法中做这个。In the Simple Text Editor app, the document object’s data is an NSString object, but UIDocument does not allow you to write strings directly to disk. So instead, you must package the string in a form that the document object can handle, namely an NSData object. You do this in thecontentsForType:error: method of your document subclass.
使用内置的文档基础设施允许你聚焦你的数据,而不是担心如何将数据写入磁盘。当你返回你的数据对象,文件对象创建一个文件协调员对象,用这个对象来读取数据到磁盘。一个文件协调员对象的使用确保你的应用独占访问该文件,当保存文件到一个云容器目录时是被需要的。如果你没有用UIDocument类,你要对创建文件协调员对象负责。Using the built-in document infrastructure allows you to focus on your data instead of worrying about how to write that data to disk. When you return your data object, the document object creates a file coordinator object and uses it to write the data to disk. The use of a file coordinator ensures that your app has exclusive access to the file and is required when saving files to an iCloud container directory. If you did not use the UIDocument class, you would be responsible for creating the file coordinator object yourself.
读取文件数据的过程类似于写得过程。所有你要的是从数据对象到loadFromContents:ofType:errror:方法检索文件的数据。最为写,你不用创建一个文件协调员或者做任何其他从提供的对象读数据。The process for reading your document data is similar to the process for writing it. All you have to do is retrieve your document data from the data object passed to the loadFromContents:ofType:error: method. As with writing, you do not have to create a file coordinator or do anything other than read your data from the provided object.
定义一个代理协议,描述文档修改Define a Delegate Protocol to Report Document Updates
当文件改变内容,文件对象必须将改变告诉其他相关的对象。其中一种方式是使用代理对象。When the contents of the document change, the document object must communicate those changes to other interested objects. One way to do so is using adelegate object.
在支持一个代理对象的第一步是定义一个该对象必须要实现的协议。对于STESimpleTextDocument类,当文件发起它的内容的改变时,你需要让代理知道。在这个应用中,代理是一个视图控制器,所以代理方法会给视图控制器一次改变它的视图的机会。The first step in supporting a delegate object is to define a protocol with the methods that object must implement. For the STESimpleTextDocument class, you need to let the delegate know when the document initiates changes to its content. In this app, the delegate is always a view controller, so the delegate method gives that view controller a chance to update its views.
对STESimpleTextDocument类,一个文件只有在从磁盘加载内容时才会改变它自己的内容。所以,唯一的方法,loadFromContents:ofType:error是必须修改来调用代理方法。所有文件相关的改变是来自外部的文件,不会造成调用dialing方法。For the STESimpleTextDocument class, a document changes its own content only when it loads content from disk. Thus, the only method that must be modified to call the delegate method is the loadFromContents:ofType:error: method. All other document-related changes come from outside of the document and do not result in calling the delegate method.
进入你的文件对象的代码之后,创建你的项目,保证可以编译。这一点,你只会看到默认的主从复合接口,引文文件对象已经没有被使用了。After entering the code for your document object, build your project to make sure everything compiles. At this point, you see only the default master-detail interface, because the document object has not yet been used
扼要重述Recap
在这一节,你会学到如何定义一个文件类,使用它类读写文件的内容。你还会学会怎样一个取消管理对象可以引发自动保存操作,如何使用一个代理来发布改变。最后,你学会如何使用一个代理协议来交流改变到其他相关的对象。下一节,你将开始创建你的应用的接口,这样,就可以展现你创建的文件。In this chapter, you learned how to define a document class and use it to read and write the contents of a file. You also learned how an undo manager object can trigger autosave operations and how to use a delegate to report changes. Finally, you learned how to use a delegate protocol to communicate changes to other interested objects. In the next chapter, you will start building the interface of your app so that you can display the documents you create.

浙公网安备 33010602011771号