Why does fast enumeration not skip the NSNumbers when I specify NSStrings?

我只是一个虾纸丫 提交于 2019-12-01 07:08:01

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);
    }
}

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.

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);
}

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.

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.

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!