NSEnumerator 的值是多少?

发布于 2024-08-24 16:26:23 字数 225 浏览 3 评论 0原文

当 为 . 。 。在 。 。 。有空吗?

具体来说,当我们可以写:

NSArray *array;
// array allocated and initialized here.
for (id obj in array) {
  // do something to the object here
}

为什么我们要使用 NSEnumerator?

when for . . . in . . . is available?

Specifically, when we can write:

NSArray *array;
// array allocated and initialized here.
for (id obj in array) {
  // do something to the object here
}

Why would we ever use an NSEnumerator?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烙印 2024-08-31 16:26:23

NSEnumerator 是在快速枚举(for/in 循环)可用之前创建的。如果您愿意,可以将其视为向后兼容。

但是使用 NSEnumerator,您可以按自定义顺序枚举集合,例如向后:(

NSEnumerator* enu = [array reverseObjectEnumerator];
id object;
while ((object = [enu nextObject])) {
 ...
}

当然,由于 NSEnumerator 也支持 for/in 循环,您可以使用更好的方法:)

for (id object in [array reverseObjectEnumerator]) {
  ...
}

或者

通过子类化 NSEnumerator 来定义自己的迭代器类,例如

#import <Foundation/Foundation.h>

@interface RangeEnumerator : NSEnumerator {
    int cur, len;
}
+(RangeEnumerator*)enumeratorWithLength:(int)length;
-(id)initWithLength:(int)length;
-(id)nextObject;
@end
@implementation RangeEnumerator
-(id)initWithLength:(int)length {
    if ((self = [super init]))
        len = length;
    return self;
}
+(RangeEnumerator*)enumeratorWithLength:(int)length {
    return [[(RangeEnumerator*)[self alloc] initWithLength:length] autorelease];
}
-(id)nextObject {
    if (cur < len)
        return [NSNumber numberWithInt:cur++];
    else
        return nil;
}
@end

int main () {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    for (NSNumber* num in [RangeEnumerator enumeratorWithLength:12])
        printf("%d\n", [num intValue]);

    [pool drain];

    return 0;
}

NSEnumerator was created before fast enumeration (for/in loop) was available. Think of it as backward-compatibility if you like.

But with NSEnumerator you can enumerate the collection in customized order, e.g. backwards:

NSEnumerator* enu = [array reverseObjectEnumerator];
id object;
while ((object = [enu nextObject])) {
 ...
}

(Of course, since NSEnumerator also supports for/in loop you can use a better way:

for (id object in [array reverseObjectEnumerator]) {
  ...
}

)

or define your own iterator class by subclassing NSEnumerator, e.g.

#import <Foundation/Foundation.h>

@interface RangeEnumerator : NSEnumerator {
    int cur, len;
}
+(RangeEnumerator*)enumeratorWithLength:(int)length;
-(id)initWithLength:(int)length;
-(id)nextObject;
@end
@implementation RangeEnumerator
-(id)initWithLength:(int)length {
    if ((self = [super init]))
        len = length;
    return self;
}
+(RangeEnumerator*)enumeratorWithLength:(int)length {
    return [[(RangeEnumerator*)[self alloc] initWithLength:length] autorelease];
}
-(id)nextObject {
    if (cur < len)
        return [NSNumber numberWithInt:cur++];
    else
        return nil;
}
@end

int main () {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    for (NSNumber* num in [RangeEnumerator enumeratorWithLength:12])
        printf("%d\n", [num intValue]);

    [pool drain];

    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文