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.

It includes the following topics:

Types

All the variables you create in WebScript are objects. Consequently, there is only one data type: id. For more information on the id type, see "The id Data Type" in the "Using WebScript" chapter.

Sending Messages

To get an object to invoke one of its methods, you send it a message. For example, the following statement:

[colorArray removeAllObjects];
tells the object colorArray to invoke its removeAllObjects method. Message expressions are enclosed in square brackets:

[receiver message];
The receiver is an object, and the message is the method you want to invoke and any arguments passed to it. The following are examples of messages with arguments:

[colorArray addObject:newColor];
[colorArray writeToFile:fileName atomically:YES];
In the first statement, newColor is an argument to the method addObject:. In the second statement, fileName and YES are both arguments to the method writeToFile:atomically:.

For more information on messages, see "Messaging in WebScript" in the "Using WebScript" chapter.

Representing Objects as Strings

You can get 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 only contains the name of the receiver's class, but most objects provide more information. For class-specific details, see the description method descriptions later in this chapter.

Mutable and Immutable Objects

Some objects are immutable; 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.

For clarity, it's best to use immutable objects wherever possible. Only use a mutable object 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. isEqual: returns YES if the receiver of the message and the specified object are equal, NO otherwise. Different types of objects determine equality in different ways. 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.

Reading from and Writing to 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:

id errorLog = [NSString stringWithContentsOfFile:errorPath];
id newErrorLog = [errorLog stringByAppendingFormat:@"%@: %@.\n",
		timeStamp, @"premature end of file."];
[newErrorLog writeToFile:errorPath atomically:YES];
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.

Writing to Files

The method writeToFile:atomically: uses the description method to obtain a human-readable string representation of the receiver. It 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 file name. If flag 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