当我指定 NSStrings 时,为什么快速枚举不跳过 NSNumbers?

发布于 2024-11-27 17:24:29 字数 1081 浏览 2 评论 0原文

我以为我知道如何使用快速枚举,但有一些我不明白的地方。如果我创建三个 NSString 对象和三个 NSNumber 对象并将它们放入 NSMutableArray 中:

NSString *str1 = @"str1";
NSString *str2 = @"str2";
NSString *str3 = @"str3";

NSNumber *nb1 = [NSNumber numberWithInt:1];
NSNumber *nb2 = [NSNumber numberWithInt:2];
NSNumber *nb3 = [NSNumber numberWithInt:3];

NSArray *array = [[NSArray alloc] initWithObjects:str1, str2, str3, nb1, nb2, nb3, nil];

那么我可以对所有 NSString 进行快速枚举 对象,如下所示:

for (NSString *str in array) {
    NSLog(@"str : %@", str);
}

在控制台中,我得到以下结果:

2011-08-02 13:53:12.873 FastEnumeration[14172:b603] str : str1
2011-08-02 13:53:12.874 FastEnumeration[14172:b603] str : str2
2011-08-02 13:53:12.875 FastEnumeration[14172:b603] str : str3
2011-08-02 13:53:12.875 FastEnumeration[14172:b603] str : 1
2011-08-02 13:53:12.876 FastEnumeration[14172:b603] str : 2
2011-08-02 13:53:12.876 FastEnumeration[14172:b603] str : 3

我仅记录了 NSString,但我为数组中的每个对象(甚至是 NSNumber>)获取了一行我不明白 为什么。快速枚举是否总是使用数组中包含的每个对象?

I thought that I knew how to use fast enumeration, but there is something I don't understand about it. If I create three NSString objects and three NSNumber objects and put them in an NSMutableArray:

NSString *str1 = @"str1";
NSString *str2 = @"str2";
NSString *str3 = @"str3";

NSNumber *nb1 = [NSNumber numberWithInt:1];
NSNumber *nb2 = [NSNumber numberWithInt:2];
NSNumber *nb3 = [NSNumber numberWithInt:3];

NSArray *array = [[NSArray alloc] initWithObjects:str1, str2, str3, nb1, nb2, nb3, nil];

then I make do fast enumeration on all NSString objects, like this:

for (NSString *str in array) {
    NSLog(@"str : %@", str);
}

In the console, I get this result :

2011-08-02 13:53:12.873 FastEnumeration[14172:b603] str : str1
2011-08-02 13:53:12.874 FastEnumeration[14172:b603] str : str2
2011-08-02 13:53:12.875 FastEnumeration[14172:b603] str : str3
2011-08-02 13:53:12.875 FastEnumeration[14172:b603] str : 1
2011-08-02 13:53:12.876 FastEnumeration[14172:b603] str : 2
2011-08-02 13:53:12.876 FastEnumeration[14172:b603] str : 3

I logged only the NSStrings, but I get a line for every object in the array, even the NSNumbers and I don't understand why. Does fast enumeration always use every object contained in an array?

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

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

发布评论

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

评论(8

梦与时光遇 2024-12-04 17:24:29

当你编写这样的 forin 循环时,它会将数组中的每个对象转换为 NSString,然后根据请求将它们打印出来。

如果您只想要 NSString,则需要编写如下内容:

for (id obj in array) {
    if ([obj isKindOfClass:[NSString class]]) {
        NSLog(@"str: %@", obj);
    }
}

When you write a forin loop like that, it casts every object in the array as an NSString, then prints them out as requested.

If you want only the NSStrings, you would need to write something like this:

for (id obj in array) {
    if ([obj isKindOfClass:[NSString class]]) {
        NSLog(@"str: %@", obj);
    }
}
我的影子我的梦 2024-12-04 17:24:29

for all 循环不知道 NSString 和 Integers 之间的区别——它只会遍历整个数组,将每个数组转换为 NSString,然后按照您的要求打印出来。

The for all loop doesn't know the difference between NSStrings and Integers -- it will simply go through the entire array, cast each as an NSString, and print them out as you asked.

£冰雨忧蓝° 2024-12-04 17:24:29

我非常确定快速枚举会返回数组中的所有对象 - 您在 for (NSString *str in array) 中所做的所有操作都是将 str 类型转换为 <代码> NSString 。在循环体中,您需要检查返回对象的类以确保它是一个 NSString

for(NSString *str in array)
{
    if([str isKindOfClass:[NSString class]])
        NSLog(@"str : %@", str);
}

I'm pretty sure that fast enumeration returns all objects in the array- all that you're doing in for (NSString *str in array) is typecasting str to an NSString. In the body of the loop you need to check the class of the returned object to make sure that it is an NSString.

for(NSString *str in array)
{
    if([str isKindOfClass:[NSString class]])
        NSLog(@"str : %@", str);
}
情未る 2024-12-04 17:24:29

Objective-C 是动态类型的,这意味着在运行时(当循环实际运行时),对象实际上都是具有不同类的一种类型 (id)。该语言允许可选的编译时静态类型,但所做的只是检查您发送的消息对于您标记的类型是否有效。它实际上并没有改变程序的行为。如果你将一个对象转换为与实际不同的类型,你所做的就是对编译器撒谎并击败它的类型检查器。

Objective-C is dynamically typed, meaning that at runtime (when the loop actually runs), objects are all effectively one type (id) with different classes. The language allows optional compile-time static typing, but all that does is check whether the messages you're sending are valid for the type you've marked. It doesn't actually change the behavior of your program. If you cast an object to be a different type than it actually is, all you're doing is lying to the compiler and defeating its type-checker.

怂人 2024-12-04 17:24:29

从 NSObject 继承的每个对象都实现了方法 - (NSString)description,Objective-C 格式字符串中的 %@ 将采用 %@ 的相应参数并调用其描述方法,NSObject 的大多数子类将实现自己的版本 - ( NSString)描述。 键入时,也会发生同样的情况。

> po anObject

当您在调试器中

Every object that descends from NSObject implements the method - (NSString)description, %@ in Objective-C formate string will take the corresponding argument for the %@ and call its description method, Most subclasses of NSObject will implement there own version of - (NSString)description. The same thing happens when you type

> po anObject

in the debugger.

不可一世的女人 2024-12-04 17:24:29
for (NSString *str in array) {

是一种枚举数组中所有元素的方法。

您期望通过指定 NSString 只能获得该类型的对象是不正确的。相反,所有对象指针都被强制转换为该类型 (NSString*)。

查看快速枚举在《Objective-C 编程语言指南》中。

for (NSString *str in array) {

is a way to enumerate through all the elements in array.

You expectative that by specifying NSString you get only the objects of that type is not correct. Rather, all the objects pointers are cast to that type (NSString*).

Have a look at Fast Enumeration in The Objective-C Programming Language guide.

纸短情长 2024-12-04 17:24:29

我不明白意外行为在哪里,在 NSMutableArray 中使用增强的 for 循环只会迭代数组中的每个对象,在您的情况下是 6,结果是正确且符合预期的。

这些数字将被转换为字符串。

I don't understand where is the unexpected behavior, using the enhanced for loop in an NSMutableArray will just iterate thru every single object in the array which in your case is 6, the result is correct and expected.

The numbers will just get casted to Strings.

与之呼应 2024-12-04 17:24:29

在快速枚举中没有类型转换,只需将指针分配给新对象

in fast enumeration no typecasting,just assigning the pointer into new object

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