当我指定 NSStrings 时,为什么快速枚举不跳过 NSNumbers?
我以为我知道如何使用快速枚举,但有一些我不明白的地方。如果我创建三个 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 NSString
s, but I get a line for every object in the array, even the NSNumber
s and I don't understand why. Does fast enumeration always use every object contained in an array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
当你编写这样的 forin 循环时,它会将数组中的每个对象转换为 NSString,然后根据请求将它们打印出来。
如果您只想要 NSString,则需要编写如下内容:
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 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.我非常确定快速枚举会返回数组中的所有对象 - 您在
for (NSString *str in array)
中所做的所有操作都是将str
类型转换为 <代码> NSString 。在循环体中,您需要检查返回对象的类以确保它是一个NSString
。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 typecastingstr
to anNSString
. In the body of the loop you need to check the class of the returned object to make sure that it is anNSString
.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.从 NSObject 继承的每个对象都实现了方法 - (NSString)description,Objective-C 格式字符串中的 %@ 将采用 %@ 的相应参数并调用其描述方法,NSObject 的大多数子类将实现自己的版本 - ( NSString)描述。 键入时,也会发生同样的情况。
当您在调试器中
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
in the debugger.
是一种枚举数组中所有元素的方法。
您期望通过指定 NSString 只能获得该类型的对象是不正确的。相反,所有对象指针都被强制转换为该类型 (
NSString*
)。查看快速枚举在《Objective-C 编程语言指南》中。
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.
我不明白意外行为在哪里,在 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.
在快速枚举中没有类型转换,只需将指针分配给新对象
in fast enumeration no typecasting,just assigning the pointer into new object