2010年5月12日水曜日

NSArray(配列クラス)

NSArray とそのサブクラスである NSMutableArray は配列と呼ばれるオブジェクトの集合を管理する。NSArray は静的な配列を生成し、NSMutableArray はダイナミック配列を生成する。

NSArray と NSMutableArray クラスは NSCopying と NSMutableCopying プロトコルに適合する。このプロトコルはある型の配列を他の型に変換することを容易とする。

NSArray と NSMutableArray クラスの繋がりの一部であり、そのため配列はNSArray または NSMutableArray クラスのインスタンスではないが、private サブクラスとなる。配列のクラスは privateのため, そのインスタンスは publicであり, その抽象スーパクラスであるNSArray と NSMutableArrayによって宣言される。

NSArrayは2つの基本となるメソッドを持つ—count と objectAtIndex:—。これらは他のメソッドのインタフェースの基本となる。count メソッドは配列の要素数を返す。 objectAtIndex: は、インデックスを用いこれらの配列にアクセスする。インデックスは0からスタートする。

The methods objectEnumerator and reverseObjectEnumerator also grant sequential access to the elements of the array, differing only in the direction of travel through the elements. These methods are provided so that arrays can be traversed in a manner similar to that used for objects of other collection classes, such as NSDictionary. See the objectEnumerator method description for a code excerpt that shows how to use these methods to access the elements of an array. In Mac OS X v10.5 and later, it is more efficient to use the fast enumeration protocol (see NSFastEnumeration).

NSArray provides methods for querying the elements of the array. The indexOfObject: method searches the array for the object that matches its argument. To determine whether the search is successful, each element of the array is sent an isEqual: message, as declared in the NSObject protocol. Another method, indexOfObjectIdenticalTo:, is provided for the less common case of determining whether a specific object is present in the array. The indexOfObjectIdenticalTo: method tests each element in the array to see whether its id matches that of the argument.

NSArray’s filteredArrayUsingPredicate: method allows you to create a new array from an existing array filtered using a predicate (see Predicate Programming Guide).

NSArray’s makeObjectsPerformSelector: and makeObjectsPerformSelector:withObject: methods let you send messages to all objects in the array. To act on the array as a whole, a variety of other methods are defined. You can create a sorted version of the array (sortedArrayUsingSelector: and sortedArrayUsingFunction:context:, extract a subset of the array (subarrayWithRange:), or concatenate the elements of an array of NSString objects into a single string (componentsJoinedByString:). In addition, you can compare two arrays using the isEqualToArray: and firstObjectCommonWithArray: methods. Finally, you can create new arrays that contain the objects in an existing array and one or more additional objects with arrayByAddingObject: and arrayByAddingObjectsFromArray:.

Arrays maintain strong references to their contents—in a managed memory environment, each object receives a retain message before its id is added to the array and a release message when it is removed from the array or when the array is deallocated. If you want a collection with different object ownership semantics, consider using CFArray Reference, NSPointerArray, or NSHashTable instead.

NSArray is “toll-free bridged” with its Core Foundation counterpart, CFArray Reference. What this means is that the Core Foundation type is interchangeable in function or method calls with the bridged Foundation object, providing you cast one type to the other. Therefore, in an API where you see an NSArray * parameter, you can pass in a CFArrayRef, and in an API where you see a CFArrayRef parameter, you can pass in an NSArray instance. This arrangement also applies to your concrete subclasses of NSArray. See Carbon-Cocoa Integration Guide for more information on toll-free bridging.

適合プロトコル



配列を生成する





    配列を初期化する



    0 件のコメント:

    コメントを投稿