Online Book Reader

Home Category

Learn Objective-C on the Mac - Mark Dalrymple [146]

By Root 1022 0
is to implement matching methods for rightward transitions. These methods are all pretty similar to the others, and are presented here without further comment, except one: you may be tempted to copy and paste the existing methods and make whatever you changes you can spot, but be careful! Some of the differences are subtle but important.

Now Build & Run, and you should see that you can flip in both directions.

What Have We Done?


Hopefully, the previous chapter and this one have given you a solid footing in a variety of Cocoa drawing techniques, including a variety of uses of Bezier curves, making your views interactive by using the mouse, and fairly painless animation with Core Animation. The scope of this book doesn’t really allow us to delve any further into these topics, especially considering that where graphics and animation are concerned, the only limits are your own imagination! We’ve given you the basic tools. If you want to do more with graphics, now it’s your turn to dig deeper into the areas that interest you most, and see what you can do with the APIs that Cocoa gives you. That’s where the fun really begins!

Chapter15

Working with Files

Most applications will need to deal with files stored on disk in one way or another. So far in this book, we really haven’t dealt much with this topic (except for a bit of discussion about Core Data and its data stores), so let’s remedy that right now. Cocoa actually includes several useful classes for dealing with files in a number of ways. There are classes that provide APIs mimicking operations the user can normally do in the Finder, and others that represent a file in an abstract way. Still other classes have built-in functionality for reading and writing files.

Implicit File Access


Several classes in Cocoa, such as NSString, NSData, NSArray, and NSDictionary, provide methods for reading data directly from a file, or writing their contents directly to a file, using just a string containing the full path to the relevant file. For instance, if you want to read the entire contents of a file into a string, you can do something as simple as this:

That code will take care of all the busy work of opening the file and reading its contents. It will even tell us what text encoding it used to interpret the contents of the file as a character string, and tell us any errors that occur. But only if we pass in non-NULL values for the second and third parameters. Apart from file-related errors, such as insufficient permissions to access the file, this method can also report back errors related to dealing with data as a character string, such as text encoding errors if the file contains binary data. Later in this chapter you’ll see this in action.

NSArray and NSDictionary have similar methods, which for some reason don’t include the sort of error-reporting that NSString does, so if they fail, they simply return a nil pointer and leave you wondering. Those methods are also much more special-purpose, since they are designed to read values from files stored in Apple’s special property-list format. One common use of NSDictionary’s dictionaryWithContentsOfFile: class method is to read data from a configuration file created in a text editor or in Xcode’s plist editor. Cocoa also includes a special-purpose class called NSPropertyListSerialization for dealing with the property-list format. If you really need to parse a property-list in a general way, with complete error reporting and more control, you can use its class method propertyListFromData:mutabilityOption:format:errorDescription: (try saying that five times in a row).

Of the classes mentioned at the start of this section, NSData provides the most general file access. It can read any sort of data from disk, and represent it as an array of bytes for you to use as you will. This is the ideal way for dealing with binary data. NSData even provides an option for hinting that the file should be mapped into virtual memory (in case you know it’s such a large file, you don’t want it all in memory at once), like

Return Main Page Previous Page Next Page

®Online Book Reader