【问题标题】:Detect the last element when using NSFastEnumeration?使用 NSFastEnumeration 时检测最后一个元素?
【发布时间】:2010-12-29 22:36:22
【问题描述】:

使用 NSFastEnumeration 时是否可以检测到最后一项?

for(NSString *str in someArray){

  //Can I detect if I'm up to the last string?

}

【问题讨论】:

    标签: iphone objective-c arrays ios


    【解决方案1】:

    是否可以检测到最后一项 何时使用 NSFastEnumeration?

    没有 100% 的准确度(或者通过将数组内容限制为完全唯一的指针,以便指针比较像另一个问题中讨论的那样工作),也没有做一堆工作导致只是用旧的方式做。

    请注意,如果您可以定位 4.0+,则可以使用enumerateWithBlock:,它同时提供项目和索引。它甚至与快速枚举一样快或更快。

    【讨论】:

    • 在我的情况下,指针是唯一的,因为它们来自过滤的数据源,但你是对的。
    【解决方案2】:

    我认为唯一的方法是老式的方法,例如:

        NSUInteger count = [someArray count];
        for (NSString *str in someArray) {
             if (--count==0) {
                  //this is the last element
             }
        }
    

    【讨论】:

    • 请解释一下if语句。我不明白它背后的逻辑。
    • --countcount 的值减一并计算为减量后的值。所以总的来说它相当于:count-=1; if (count==0) { ... }
    【解决方案3】:

    在循环结束时,“str”仍将指向最后一个元素。你需要做什么?

    【讨论】:

    猜你喜欢
    • 2019-04-08
    • 2020-08-04
    • 1970-01-01
    • 2019-04-03
    • 1970-01-01
    • 2011-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多