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

前端 未结 8 1280
慢半拍i
慢半拍i 2021-01-14 02:27

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 NSNum

8条回答
  •  梦谈多话
    2021-01-14 02:33

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

提交回复
热议问题