

Creating Instances of Classes
The section "Variables" told you how to declare variables, which represent objects. Before you use a variable, you must first create the object, or instantiate the class that defines the object. In WebScript, there are two different ways to create objects. The first approach, which applies only to the most commonly used classes, is to initialize with constant objects as described in the section "Assigning Values to Variables". The second approach, which applies to all classes, is to use creation methods.Here are some examples of using creation methods to create mutable and immutable NSString, NSArray, and NSDictionary objects:
// Create a mutable string string = [NSMutableString stringWithFormat:@"The string is %@", aString]; // Create an immutable string string = [NSString stringWithFormat:@"The string is %@", aString]; // Create a mutable array array = [NSMutableArray array]; anotherArray = [NSMutableArray arrayWithObjects:@"Marsha", @"Greg", @"Cindy", nil]; // Create an immutable array array = [NSArray arrayWithObjects:@"Bobby", @"Jan", @"Peter", nil]; // Create a mutable dictionary dictionary = [NSMutableDictionary dictionary]; // Create an immutable dictionary id stooges = [NSDictionary dictionaryWithObjects:@("Mo", "Larry", "Curley") forKeys:@("Stooge1", "Stooge2", "Stooge3")];The following examples show how you can create and work with NSCalendarDates, which are always immutable:
// Using the creation method date, create an NSCalendarDate instance // 'now' that contains the current date and time now = [NSCalendarDate date]; // Return a string representation of 'now' using a format string dateString = [now descriptionWithCalendarFormat:@"%B %d, %Y"]; // Using the creation method dateWithString:, create an NSCalendarDate // instance 'newDate' from 'dateString' newDate = [NSCalendarDate dateWithString:dateString calendarFormat:@"%B %d, %Y"]; // Return a new date in which newDate's day field is decremented date = [newDate addYear:0 month:0 day:-1 hour:0 minute:0 second:0];For a detailed discussion of these classes and a more complete listing of methods, see the chapter "WebScript Programmer's Quick Reference to Foundation Classes".
Table of Contents
Next Section