NSArray 数组类(Xcode文档翻译)

来源:互联网 发布:博客关注网站软件 编辑:程序博客网 时间:2024/06/07 01:17

NSArray


Inherits from 继承自 NSObject

Comforms to 遵循协议 NSFastEnumeration, NSObject, NSCopying, NSMutableCopying, NSSecureCoding



Overview

NSArray and its subclass NSMutableArray manage ordered collections of objects called arraysNSArraycreates static arrays, and NSMutableArray creates dynamic arrays. You can use arrays when you need an ordered collection of objects.

NSArray类和他的子类NSMutableArray管理着一组称之为数组的对象的有序集合。NSArray创建静态数组,NSMutableArray创建动态数组。当你需要一个对象的有序集合时,可以使用数组来完成。

NSArray is “toll-free bridged” with its Core Foundation counterpart, CFArrayRef. See “Toll-Free Bridging” for more information on toll-free bridging.

NSArray类同CoreFoundation框架的CFArrayRef类型是“对象桥接”的。关于“对象桥接”的更多信息请参考“Toll-Free Bridging”一文。


Subclassing Notes

定义子类的注意事项


There is typically little reason to subclass NSArray. The class does well what it is designed to do—maintain an ordered collection of objects. But there are situations where a custom NSArray object might come in handy. Here are a few possibilities:

一般情况下很少需要创建NSArray的子类。对于维护一个有序的对象集合,这个类本身已经可以完成得很好了。但是也有自定义NSArray类派上用场得情况。以下列举几种可能性:

Changing how NSArray stores the elements of its collection. You might do this for performance reasons or for better compatibility with legacy code.

改变NSArray内部得元素保存的方式。你可能出于性能原因或为了与先前遗留代码更好的兼容这样做。

Acquiring more information about what is happening to the collection (for example, statistics gathering).

获取集合当前的更多信息(例如,统计数据收集)


Methods to Override

重载的方法

Any subclass of NSArray must override the primitive instance methods count and objectAtIndex:. These methods must operate on the backing store that you provide for the elements of the collection. For this backing store you can use a static array, a standard NSArray object, or some other data type or mechanism. You may also choose to override, partially or fully, any other NSArray method for which you want to provide an alternative implementation.

NSArray的任何子类都必须重写原始的实例方法: count和objectAtIndex:。这些方法必须运行在你为元素集合提供的后备存储器上。在这个后备存储器上你可以使用一个静态数组,一个标准的NSArray对象,或者其它数据类型或机制。你也可以选择重写部分或全部的,其它想要有替代实现方式的NSArray方法。


You might want to implement an initializer for your subclass that is suited to the backing store that the subclass is managing. If you do, your initializer must invoke one of the designated initializers of the NSArrayclass, either init or initWithObjects:count:. The NSArray class adopts the NSCopying,NSMutableCopying, and NSCoding protocols; if you want instances of your own custom subclass created from copying or coding, override the methods in these protocols.

你可以为你的子类实现一个初始化方法,去适应子类管理的后备存储。如果你这样做了,那么你的初始化方法需要调用一个NSArray类的指派初始化方法:init或initWithObjects:count:。NSArray类遵循NSCopying, NSMutableCopying以及NSCoding协议;如果你希望通过copy或code来实例化你的自定义子类,就需要重写这些协议。


Remember that NSArray is the public interface for a class cluster and what this entails for your subclass. You must provide the storage for your subclass and implement the primitive methods that directly act on that storage.

需要记住的是,NSArray是一个公共的类簇接口,对于你的子类也如此。你必须给你的子类提供存储,并实现直接作用于这个存储的原始方法。


Alternatives to Subclassing

定义子类的替代方法

Before making a custom class of NSArray, investigate NSPointerArray and the corresponding Core Foundation type, CFArray Reference. Because NSArray and CFArray are “toll-free bridged,” you can substitute a CFArray object for a NSArray object in your code (with appropriate casting). Although they are corresponding types, CFArray and NSArray do not have identical interfaces or implementations, and you can sometimes do things with CFArray that you cannot easily do with NSArray. For example, CFArray provides a set of callbacks, some of which are for implementing custom retain-release behavior. If you specify NULLimplementations for these callbacks, you can easily get a non-retaining array.

在自定义NSArray类之前,需要先探讨以下NSPointerArray和相应的CoreFoundation类型 CFArray Reference. 因为NSArray和CFArray是“对象桥接”的,你可以在代码中(通过适当的映射)用一个CFArray对象替代NSArray对象。尽管他们是相对应的类型,CFArray和NSArray没有相同的接口或实现,而有时你可以轻松的处理CFArray却不能轻而易举的处理NSArray。例如,CFArray提供了一组回调函数,有些用于实现自定义retain-release行为。如果你指定这些回调函数为NULL实现,你可以很容易获得一个没有retain的数组。

If the behavior you want to add supplements that of the existing class, you could write a category on NSArray. Keep in mind, however, that this category will be in effect for all instances of NSArray that you use, and this might have unintended consequences. Alternatively, you could use composition to achieve the desired behavior.

如果你想给一个已经存在的类添加补充,你可以给NSArray写一个类目。请记住,这个类目会作用于所有的NSArray类的实例,这可能会导致意想不到的后果。而你可以用复合模式去达到期望的行为效果。


Tasks

任务

Creating an Array

创建一个字符串

+ array

+ arrayWithArray:

+ arrayWithContentsOfFile:

+ arrayWithContentsOfURL:

+ arrayWithObject:

+ arrayWithObjects:

+ arrayWithObjects:count:


Initializing an Array

初始化一个字符串

– init

– initWithArray:

– initWithArray:copyItems:

– initWithContentsOfFile:

– initWithContentsOfURL:

– initWithObjects:

– initWithObjects:count:


Querying an Array

查找一个数组

– containsObject:

– count

– getObjects:range:

– firstObject

– lastObject

– objectAtIndex:

– objectAtIndexedSubscript:

– objectsAtIndexes:

– objectEnumerator

– reverseObjectEnumerator

– getObjects: Deprecated in iOS 4.0


Finding Objects in an Array

在数组中找到对象

– indexOfObject:

– indexOfObject:inRange:

– indexOfObjectIdenticalTo:

– indexOfObjectIdenticalTo:inRange:

– indexOfObjectPassingTest:

– indexOfObjectWithOptions:passingTest:

– indexOfObjectAtIndexes:options:passingTest:

– indexesOfObjectsPassingTest:

– indexesOfObjectsWithOptions:passingTest:

– indexesOfObjectsAtIndexes:options:passingTest:

– indexOfObject:inSortedRange:options:usingComparator:


Sending Messages to Elements

向各个元素发送消息

– makeObjectsPerformSelector:

– makeObjectsPerformSelector:withObject:

– enumerateObjectsUsingBlock:

– enumerateObjectsWithOptions:usingBlock:

– enumerateObjectsAtIndexes:options:usingBlock:


Comparing Arrays

比较字符串

– firstObjectCommonWithArray:

– isEqualToArray:


Deriving New Arrays

派生新数组

– arrayByAddingObject:

– arrayByAddingObjectsFromArray:

– filteredArrayUsingPredicate:

– subarrayWithRange:


Sorting

排序

– sortedArrayHint

– sortedArrayUsingFunction:context:

– sortedArrayUsingFunction:context:hint:

– sortedArrayUsingDescriptors:

– sortedArrayUsingSelector:

– sortedArrayUsingComparator:

– sortedArrayWithOptions:usingComparator:


Working with String Elements

处理字符串元素

– componentsJoinedByString:


Creating a Description

创建描述

– description

– descriptionWithLocale:

– descriptionWithLocale:indent:

– writeToFile:atomically:

– writeToURL:atomically:


Collecting Paths

收集路径

– pathsMatchingExtensions:


Key-Value Observing

KVO

– addObserver:forKeyPath:options:context:

– removeObserver:forKeyPath:

– removeObserver:forKeyPath:context:

– removeObserver:fromObjectsAtIndexes:forKeyPath:context:

– addObserver:toObjectsAtIndexes:forKeyPath:options:context:

– removeObserver:fromObjectsAtIndexes:forKeyPath:


Key-Value Coding

KVC

– setValue:forKey:

– valueForKey:



0 0