Online Book Reader

Home Category

Cocoa Programming for Mac OS X - Aaron Hillegass [59]

By Root 882 0
Identifier, Icon, and Extensions to be identical to the settings you made in the Document Type. For Conforms To, type public. data (Figure 10.5).

Figure 10.5. Configuring the Document Type and Exported UTI

Build and run your application. You should be able to save data to a file and read it in again. In Finder, the txt.icns icon will be used as the icon for your .rsmn files.

An application is a directory. The directory contains the NIB files, images, sounds, and executable code for the application. In Terminal, try the following:

> cd /Applications/TextEdit.app/Contents

> ls

You will see three interesting things.

1. The Info.plist file, which includes the information about the application, its file types, and associated icons. Finder uses this information.

2. The MacOS/ directory, which contains the executable code.

3. The Resources/ directory, which has the images, sounds, and NIB files that the application uses. You will see localized resources for several different languages.

For the More Curious: Preventing Infinite Loops


The astute reader may be wondering: “If object A causes object B to be encoded, and object B causes object C to be encoded, and then object C causes object A to be encoded again, couldn’t it just go around and around in an infinite loop?” It would, but NSKeyedArchiver was designed with this possibility in mind.

When an object is encoded, a unique token is also put onto the stream. Once archived, the object is added to the table of encoded objects under that token. When told to encode the same object again NSKeyedArchiver simply puts a token in the stream.

When it decodes an object from the stream NSKeyedUnarchiver puts both the object and its token in a table. If it finds a token with no associated data, the unarchiver knows to look up the object in the table instead of creating a new instance.

This idea led to the method in NSCoder that often confuses developers when they read the documentation:

- (void)encodeConditionalObject:(id)anObject forKey:(NSString *)aKey

This method is used when object A has a pointer to object B, but object A doesn’t really care if B is archived. However, if another object has archived B, A would like the token for B put into the stream. If no other object has archived B, it will be treated like nil.

For example, if you were writing an encodeWithCoder: method for an Engine object (Figure 10.6), it might have an instance variable called car that is a pointer to the Car object that it is part of. If you are archiving only the Engine, you wouldn’t want the entire Car archived. But if you were archiving the entire Car, you would want the car pointer set. In this case, you would make the Engine object encode the car pointer conditionally.

Figure 10.6. Conditional Encoding Example

For the More Curious: Creating a Protocol


Creating your own protocol is very simple. Here is a protocol with two methods. The protocol would typically be in a file called Foo.h:

@protocol Foo

- (void)fido:(int)x;

- (float)rex;

@end

With Objective-C 2.0, @optional was added to the protocol grammar. Now you can indicate which methods in a protocol are required and which are optional:

@protocol Foo

- (void)fido:(int)x;

- (float)rex;

@optional

- (int)rover;

- (void)spot:(int)x;

@end

In this example, fido: and rex are required; rover and spot: are optional.

If you had a class that wanted to implement the Foo protocol and the NSCoding protocol, the class would look like this:

#import "Spunky.h"

#import "Foo.h"

@interface ZsaZsa : Spunky

...etc...

@end

A class doesn’t have to redeclare any method it inherits from its superclass, nor does it have to redeclare any of the methods from the protocols it implements. Thus, in our example, the interface file for the class ZsaZsa is not required to list any of the methods in Spunky or Foo or NSCoding.

For the More Curious: Automatic Document Saving


In Mac OS X Lion Apple introduced automatic document-saving support to Cocoa. With

Return Main Page Previous Page Next Page

®Online Book Reader