Objective C:使用快速枚举时的最后一个对象?

时间:2010-12-27 22:06:12

标签: iphone objective-c for-loop fast-enumeration

在使用快速枚举时,知道何时到达数组中的最后一个对象的最佳方法是什么?有没有比增加int然后将其与数组长度进行比较更好的方法?

5 个答案:

答案 0 :(得分:15)

如果你从一个数组开始,那就输出最后一个元素(这是一个特定的调用),并将你在枚举器中得到的每个元素与之比较。类似的东西:

id lastEl = [myArray lastObject];

for ( id anEl in myArray )
{
   if ( anEl == lastEl )
     .....  // do the last thing
}

比较成本与计数器的成本相同,但是当你忘记增加计数器时,你没有增加计数器的成本或总是出现的错误。

答案 1 :(得分:2)

通常,快速枚举意味着您没有使用计数器......

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocFastEnumeration.html

另一种形式是没有柜台......

NSEnumerator *enumerator = [objectSet objectEnumerator];
id setObject;
while ((setObject = [enumerator nextObject]) != nil)
{
    ...
}

答案 2 :(得分:1)

如果性能测试负责here,则快速枚举和块枚举之间没有太大的性能差异。或者bbum说here"基于块的枚举允许集合类以最快遍历本机存储格式的速度枚举内容。 "

然后我建议使用块枚举,这样你就可以同时获得索引和对象,并且可以进行比较。

答案 3 :(得分:0)

我不相信有任何(简单)方法可以做到这一点 - 这是使用快速枚举的权衡之一。因此,当您需要知道要枚举的项目的索引时,您需要按照建议创建一个整数。

那就是说,当你使用快速枚举器等时,你仍然可以从这个事实中受益。

答案 4 :(得分:0)

您还可以对块使用枚举:

</dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http.Formatting" culture="neutral" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>

      <dependentAssembly>
        <assemblyIdentity name="System.Web.Cors" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>


      <dependentAssembly>
    <assemblyIdentity name="Autofac" publicKeyToken="17863af14b0044da" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
    </dependentAssembly>

    </assemblyBinding>

如果你知道你的数组包含哪种对象,最好用适当的类替换 Could not load file or assembly 'System.Web.Cors, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.IO.FileNotFoundException: Could not load file or assembly 'System.Web.Cors, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. Source Error: Line 42: config.DependencyResolver = new AutofacWebApiDependencyResolver(container); Line 43: AreaRegistration.RegisterAllAreas(); Line 44: GlobalConfiguration.Configure(WebApiConfig.Register); Line 45: FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); Line 46: RouteConfig.RegisterRoutes(RouteTable.Routes);