对于...声明Objective-C [英] For...in statement Objective-C

查看:97
本文介绍了对于...声明Objective-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Objective-C,并且遇到了这个"for ... in"声明.我搜索了它,但仍然不知道它是如何工作的.有人可以以友好的方式向我解释此声明的工作原理吗?

I am studying Objective-C and I came across this "for...in" statement. I searched for it but i still don't get how it works. Could someone explain to me in a noob-friendly how this statement works?

推荐答案

请参见通常,通常会有一个数组,并且可以使用方便的循环获取数组中的每个项目,而不必使用NSEnumerator或整数计数变量.它使您的代码更加简洁,可以在数组中请求每个NSString,而不必为循环的每次遍历使用objectAtIndex分配给变量.

Basically you'd have, usually, an array, and you can obtain each item in the array with a handy loop instead of using NSEnumerator or an integer count variable. It makes your code much cleaner to ask for each NSString in your array rather than to have to assign to a variable using objectAtIndex for each pass of your loop.

比较:

for (NSString *string in myArray)
{
    // do stuff...
}

收件人:

for (int i = 0; i < [myArray count]; i++)
{
    NSString *string = [myArray objectAtIndex:i];
    // Do stuff...
}

这篇关于对于...声明Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆