Difference between revisions of "NSArray"

From GNUstepWiki
Jump to navigation Jump to search
m (added a reference to the KVC)
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Well ... array.
+
[http://www.gnustep.org/resources/documentation/Developer/Base/Reference/NSArray.html#class$NSArray NSArray] is an immutable, integer indexed, array of objects.
 +
 
 +
== 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", nil];
 +
 
 +
When creating arrays remember that any kind of object can be included, but only objects can be included. Note that <code>nil</code> values cannot be included in an array; you should use the [[NSNull|<code>[NSNull null]</code>]] dummy object instead.
 +
 
 +
Note that when you provide a variable-length list of objects in Objective-C, you need to "terminate" the list with <code>nil</code>. Otherwise, the function has no way of knowing how many arguments there are.
 +
 
 +
=== 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", nil];
 +
  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 ==
 
== Code chunks ==
Line 26: Line 54:
 
     return AUTORELEASE(retval);
 
     return AUTORELEASE(retval);
 
  }
 
  }
 +
 +
== See Also ==
 +
 +
*[[NSMutableArray]]
 +
*[[NSEnumerator]]
 +
 +
 +
[[Category:Foundation]]
 +
[[Category:Snippets]]

Latest revision as of 08:00, 12 May 2009

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

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", nil];

When creating arrays remember that any kind of object can be included, but only objects can be included. Note that nil values cannot be included in an array; you should use the [NSNull null] dummy object instead.

Note that when you provide a variable-length list of objects in Objective-C, you need to "terminate" the list with nil. Otherwise, the function has no way of knowing how many arguments there are.

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", nil];
 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