Data types – Apple WebObjects 3.5 User Manual

Page 174

Advertising
background image

Chapter 10

The WebScript Language

174

// 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” (page 187).

Data Types

Several of the examples in this chapter show how you can specify a data type
when you define a method or variable. For example:

NSString *myString = @"This is my string.";

- (NSString *)appendString:(NSString *)aString {

NSString *returnString = [NSString

stringWithFormat:@"%@ %@", myString, aString];

return returnString;

}

Advertising