Main Menu
Lecture 1
Lecture 3
Lecture 4
Lecture 5
Lecture 6
Lecture 7
 

 

 

Foundation Classes

As part of WebObjects you have access to the Java version of Apple's foundation classes.

Mutable

You can change their contents

Immutable

Set up initially and can't change

Allow speed optimisations

Can safely be shared among many objects, which reduces copying overhead

Ordered Lists

  • NSArray - filled and then made immutable
    • Array itself is immutable - not its contents
    • has methods to examine but not alter contents
    • get the number of objects in an array
      • - public int count();
    • get object at index
      • - public Object objectAtIndex(int index);
    • need to cast to tell object type
      • - Customer sc = (Customer)customers.objectAtIndex(4);
    • get an enumeration
      • - Enumeration en = customers.objecEnumerator();

       

  • NSMutableArray
    • subclass of NSArray
    • adds methods to add and remove objects from array
    • analogous to Java Vector class
    • default constructor constructs an empty array
    • create mutable arrays
      • - NSMutableArray cusotmers = new NSMutableArray();
    • add objects
      • - customers.addObject(customer);
    • remove objects
      • - customers.removeObjectAtIndex(5);
      • - customers.removeObject(Object);
    • copy immutable to mutable array
      • - NSMutableArray copy = new NSMutableArray(original);
Dictionaries

Associates keys (typically strings) with object values

Analogous to Java Hashtable class

Can use any type of object as keys and store any type as values

hashCode and equals messages are sent to the key objects to determine where they should be stored

  • NSDictionary -
    • immutable
    • get the number of objects in a dictionary
      • - int counter = customers.count();
    • get object for a specific key
      • - Customer sc = (Customer)customers.objectForKey("Brown");
    • Rember to always put " " around literal strings
    • get an enumeration
      • - Enumeration en = customers.objecEnumerator();
      • - Enumeration en = customers.keyEnumerator();

       

  • NSMutableDictionary
    • stores key-value pairs
    • subclass of NSDictionary
    • create mutable dictionary
      • - NSMutableDictionary cusotmers = new NSMutableDictionary();
    • add objects
      • - customers.setObjectForKey(customer, "Johns");
    • remove objects
      • - customers.removeObjectForKey("White");
    • copy immutable to mutable array
      • - NSMutableDictionary copy = new NSMutableDictionary(original);

Example

Order in a shopping cart