Objective-C 编程语言官网文档(八)-快速枚举

声明:本文档仅为个人学习过程中顺手翻译之作,方便开发的同胞借鉴参考。如有觉得译的不好不到位的地方,欢迎指正,将及时做出更正

尽量尊重原文档,因为首次Objective-C,有些地方可能直译了没有注意该语言的专有词,希望指正。如需转载,请注明出处


我的编程环境:

IDE:XCODE4.3.1

OS: MAC OS X 10.7.4

文章来译自:http://developer.apple.com/


快速枚举

快速枚举是一种语言特性,让我们可以高效并且安全的使用简明的语法来迭代集合的内容。

for…语法

快速枚举的语法定义如下

for ( Type newVariable in expression ) { statements }

或者

Type existingItem;
for ( existingItem in expression ) { statements }

在上述两种情况中,表达式生成了一个遵循 NSFastEnumeration 协议的对象 (参见 “Adopting Fast Enumeration”). 迭代的变量在每次循环中为声明的对象设置 。当循环结束时,迭代的变量被设为 nil 。要是循环提前结束,那么迭代变量被遗弃,指向最后迭代的对象。

使用快速枚举的好处:

  • 枚举相比其它方式更加高效,例如 NSEnumerator.

  • 语法更加简明

  • 枚举的使用是“安全的”—枚举器有一个突变守卫,因此当你在枚举进行中试图驱修改集合时,就会有一个异常被抛出。

因为迭代过程中的对象的改变是禁止的,故此你可以并发的执行多个迭代。

另一方面,这个特性的行为很像一个标准的 for 循环。你可以使用 break 来终止迭代或者使用 continue 来跳出本次循环跳到下个元素。

采用(适配)快速枚举

如果一个类的实例提供了访问其它对象集合的方法,那么这个类就可以采用 NSFastEnumeration 协议. 在 Foundation 框架中的集合类—NSArrayNSDictionary 以及 NSSet—就采用了这个协议,就像 NSEnumerator. 很显然,在 NSArray 和 NSSet 中,枚举是针对它们的内容。对于其它的类,响应的文档应当明确哪个属性是用来迭代的。例如,NSDictionary 以及 Core Data 类, NSManagedObjectModel 提供了对快速迭代的支持; NSDictionary 枚举它的键, NSManagedObjectModel 枚举它的实体。

快速枚举的使用

下面的例子向我们展示了如何 NSArray 和 NSDictionary 对象是如何使用快速枚举的.

NSArray *array = [NSArray arrayWithObjects:
        @"one", @"two", @"three", @"four", nil];
 
for (NSString *element in array) {
    NSLog(@"element: %@", element);
}
 
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
    @"quattuor", @"four", @"quinque", @"five", @"sex", @"six", nil];
 
NSString *key;
for (key in dictionary) {
    NSLog(@"English: %@, Latin: %@", key, [dictionary objectForKey:key]);
}

你也可以使用 NSEnumerator 对象来做快速枚举

NSArray *array = [NSArray arrayWithObjects:
        @"one", @"two", @"three", @"four", nil];
 
NSEnumerator *enumerator = [array reverseObjectEnumerator];
for (NSString *element in enumerator) {
    if ([element isEqualToString:@"three"]) {
        break;
    }
}
 
NSString *next = [enumerator nextObject];
// next = "two"

如果你想要使用下标,那么你定义一个变量,然后在枚举里自增计数就好了

NSArray *array = <#Get an array#>;
NSUInteger index = 0;
 
for (id element in array) {
    NSLog(@"Element at index %u is: %@", index, element);
    index++;
}

英文原文:点击打开链接

Fast Enumeration

Fast enumeration is a language feature that allows you to efficiently and safely enumerate over the contents of a collection using a concise syntax.

The for…in Syntax

The syntax for fast enumeration is defined as follows:

for ( Type newVariable in expression ) { statements }

or

Type existingItem;
for ( existingItem in expression ) { statements }

In both cases, expression yields an object that conforms to the NSFastEnumeration protocol (see “Adopting Fast Enumeration”). The iterating variable is set to each item in the returned object in turn, and the code defined bystatements is executed. The iterating variable is set to nil when the loop ends by exhausting the source pool of objects. If the loop is terminated early, the iterating variable is left pointing to the last iteration item.

There are several advantages to using fast enumeration:

  • The enumeration is considerably more efficient than, for example, using NSEnumerator directly.

  • The syntax is concise.

  • Enumeration is “safe”—the enumerator has a mutation guard so that if you attempt to modify the collection during enumeration, an exception is raised.

Because mutation of the object during iteration is forbidden, you can perform multiple enumerations concurrently.

In other respects, the feature behaves like a standard for loop. You can use break to interrupt the iteration andcontinue to advance to the next element.

Adopting Fast Enumeration

Any class whose instances provide access to a collection of other objects can adopt the NSFastEnumerationprotocol. The collection classes in the Foundation framework—NSArrayNSDictionary, and NSSet—adopt this protocol, as does NSEnumerator. It should be obvious that in the cases of NSArray and NSSet the enumeration is over their contents. For other classes, the corresponding documentation should make clear what property is iterated over—for example, NSDictionary and the Core Data class NSManagedObjectModel provide support for fast enumeration; NSDictionary enumerates its keys, and NSManagedObjectModel enumerates its entities.

Using Fast Enumeration

The following code example illustrates using fast enumeration with NSArray and NSDictionary objects.

NSArray *array = [NSArray arrayWithObjects:
        @"one", @"two", @"three", @"four", nil];
 
for (NSString *element in array) {
    NSLog(@"element: %@", element);
}
 
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
    @"quattuor", @"four", @"quinque", @"five", @"sex", @"six", nil];
 
NSString *key;
for (key in dictionary) {
    NSLog(@"English: %@, Latin: %@", key, [dictionary objectForKey:key]);
}

You can also use NSEnumerator objects with fast enumeration, as illustrated in this example:

NSArray *array = [NSArray arrayWithObjects:
        @"one", @"two", @"three", @"four", nil];
 
NSEnumerator *enumerator = [array reverseObjectEnumerator];
for (NSString *element in enumerator) {
    if ([element isEqualToString:@"three"]) {
        break;
    }
}
 
NSString *next = [enumerator nextObject];
// next = "two"

For collections or enumerators that have a well-defined order—such as an NSArray or an NSEnumerator instance derived from an array—the enumeration proceeds in that order, so simply counting iterations gives you the proper index into the collection if you need it.

NSArray *array = <#Get an array#>;
NSUInteger index = 0;
 
for (id element in array) {
    NSLog(@"Element at index %u is: %@", index, element);
    index++;
}

下载方式:https://pan.quark.cn/s/c9b9b647468b ### 初级JSP程序设计教程核心内容解析#### 一、JSP基础概述JSP(JavaServer Pages)是由Sun Microsystems公司创建的一种动态网页技术规范,主要应用于构建动态网站及Web应用。JSP技术使得开发者能够将动态数据与静态HTML文档整合,从而实现网页内容的灵活性和可变性。##### JSP的显著特性:1. **动态与静态内容的分离**:JSP技术支持将动态数据(例如数据库查询结果、实时时间等)嵌入到静态HTML文档中。这种设计方法增强了网页的适应性和可维护性。2. **易用性**:开发者可以利用常规的HTML编辑工具来编写静态部分,并通过简化的标签技术将动态内容集成到页面中。3. **跨平台兼容性**:基于Java平台的JSP具有优良的跨操作系统运行能力,能够在多种不同的系统环境中稳定工作。4. **强大的后台支持**:JSP能够通过JavaBean组件访问后端数据库及其他资源,以实现复杂的数据处理逻辑。5. **执行效率高**:JSP页面在初次被请求时会被转换为Servlet,随后的请求可以直接执行编译后的Servlet代码,从而提升了服务响应的效率。#### 二、JSP指令的运用JSP指令用于设定整个JSP页面的行为规范。这些指令通常放置在页面的顶部,向JSP容器提供处理页面的相关指导信息。##### 主要的指令类型:1. **Page指令**: - **语法结构**:`<%@ page attribute="value" %>` - **功能**:定义整个JSP页面的运行特性,如设定页面编码格式、错误处理机制等。 - **实例**: ...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值