NSArray

From GNUstepWiki

(Difference between revisions)
Revision as of 00:13, 5 April 2007
Kendall (Talk | contribs)
Creating Arrays - added - See Also - added
← Previous diff
Revision as of 19:08, 22 October 2007
Geroldr (Talk | contribs)

Next diff →
Line 1: Line 1:
-[http://www.gnustep.org/resources/documentation/Developer/Base/Reference/NSArray.html#class$NSArray NSArray] is a immutable, integer indexed, array of objects.+[http://www.gnustep.org/resources/documentation/Developer/Base/Reference/NSArray.html#class$NSArray NSArray] is an immutable, integer indexed, array of objects.
== Creating Arrays == == Creating Arrays ==
Line 5: Line 5:
=== From a List === === From a List ===
-The most common method to create a array is to just pass it a list:+The most common method to create an array is to just pass it a list:
NSArray *array = [NSArray arrayWithObjects: @"John", @"Bob", [NSNull null], @"Jane"]; NSArray *array = [NSArray arrayWithObjects: @"John", @"Bob", [NSNull null], @"Jane"];
Line 11: Line 11:
When creating arrays remember that any kind of object can be included, but only objects can be included. When creating arrays remember that any kind of object can be included, but only objects can be included.
-=== Copy Array ===+=== Copy an Array ===
A copy of an array can be done simply by doing the following: A copy of an array can be done simply by doing the following:
Line 18: Line 18:
NSArray *array2 = [NSArray arrayWithArray: array1]; NSArray *array2 = [NSArray arrayWithArray: array1];
-=== From a Array ===+=== From an Array ===
An array object can be made with a regular array of objects, as shown in the following example: An array object can be made with a regular array of objects, as shown in the following example:

Revision as of 19:08, 22 October 2007

NSArray is an immutable, integer indexed, array of objects.

Contents

Creating Arrays

From a List

The most common method to create an array is to just pass it a list:

 NSArray *array = [NSArray arrayWithObjects: @"John", @"Bob", [NSNull null], @"Jane"];

When creating arrays remember that any kind of object can be included, but only objects can be included.

Copy an Array

A copy of an array can be done simply by doing the following:

 NSArray *array1 = [NSArray arrayWithObjects: @"John", @"Bob", [NSNull null], @"Jane"];
 NSArray *array2 = [NSArray arrayWithArray: array1];

From an Array

An array object can be made with a regular array of objects, as shown in the following example:

 id array1[4] = {@"John", @"Bob", [NSNull null], @"Jane"};
 NSArray *array2 = [NSArray arrayWithObjects: array1 count: 4];

Count being the number of objects from the array to be used.

Code chunks

Create index dictionary

Following NSArray category method creates a dictionary which works as an index by an attribute of contained objects. It uses Key Value Coding.

- (NSDictionary *)indexDictionaryForKey:(NSString *)key
{
   NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
   NSDictionary        *retval;
   NSEnumerator        *enumerator;
   id                   object;

   enumerator = [self objectEnumerator];

   while( (object = [enumerator nextObject]) )
   {
       [dict setObject:object forKey:[object valueForKey:key]];
   }

   retval = [[NSDictionary alloc] initWithDictionary:dict];
   RELEASE(dict);
   
   return AUTORELEASE(retval);
}

See Also