Documentation Archive Developer
Search
Table of Contents Previous Section

Foundation Objects

This section provides an overview of some of the topics, techniques, and conventions you use when programming with Foundation objects.

Representing Objects as Strings

You can obtain a human-readable string representation of any object by sending it a description message. This method is particularly useful for debugging. In some cases, the string returned from description contains only the class name of the object that received the message (the receiver). Most objects, however, provide more information. For class-specific details, see the description method descriptions later in this chapter.

Mutable and Immutable Objects

Some objects are immutable; that is, once they are created, they can't be modified. Other objects are mutable. They can be modified at any time. When you create an object, you can often choose to create it as either immutable or mutable. Three kinds of objects discussed in this chapter-strings, arrays, and dictionaries-have both immutable and mutable versions.

It's best to use immutable objects whenever possible. Use a mutable object only if you need to modify its contents after you create it.

Determining Equality

You can determine if two objects are equal using the isEqual: method. This method returns YES if the receiver of the message and the specified object are equal, and NO otherwise. The definition of equality depends on the object's type. For example, array objects define two arrays as equal if they contain the same contents. For more information, see the isEqual: method descriptions later in this chapter.

Writing to and Reading From Files

Strings, arrays, and dictionaries-three of the classes discussed in this chapter-provide methods for writing to and reading from files. The method writeToFile:atomically: writes a textual description of the receiver's contents to a specified path name, and corresponding class-specific creation methods-stringWithContentsOfFile:, arrayWithContentsOfFile:, and dictionaryWithContentsOfFile:-create an object from the contents of a specified file.

For example, the following code excerpt reads the contents of an error log stored in a file, appends a new error to the log, and saves the updated log to the same file:

	id errorLog = [NSString stringWithContentsOfFile:errorPath];
	id newErrorLog = [errorLog stringByAppendingFormat:@"%@: %@.\n",
		timeStamp, @"premature end of file."];
	[newErrorLog writeToFile:errorPath atomically:YES];

Writing to Files

To write to a file, use the method writeToFile:atomically:. It uses the description method to obtain a human-readable string representation of the receiver and then writes the string to the specified file. The resulting file is suitable for use with classNameWithContentsOfFile: methods. This method returns YES if the file is written successfully, and NO otherwise.

If the argument for atomically: is YES, the string representation is first written to an auxiliary file. Then the auxiliary file is renamed to the specified filename. If the argument is NO, the object is written directly to the specified file. The YES option guarantees that the specified file, if it exists at all, won't be corrupted even if the system should crash during writing.

When writeToFile:atomically: fails, it returns NO. If this happens, check the permissions on the specified file and its directory. The most common cause of write failures is that the process owner doesn't have the necessary permissions to write to the file or its directory. If the argument for atomically: is NO, it's sufficient to grant write permissions only on the file.

Note: The configuration of your HTTP server determines the user who owns autostarted applications.

Reading From Files

The string, array, and dictionary classes provide methods of the form classNameWithContentsOfFile:. These methods create a new object and initialize it with the contents of a specified file, which can be specified with a full or relative pathname.

Table of Contents Next Section