It includes the following topics:
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. 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.
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.
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.
Table of Contents Next Section