无法让 NSEnumerator 显示数组中的下一项
我是 Objective-C 的新手,我已经在网上研究了几周了。每个站点上的几乎每个示例都是相同的,并且并没有完全让我清楚如何将其集成到 Xcode 4 应用程序的代码中。
随处可见的例子是:
NSEnumerator* theEnum = [some_array objectEnumerator]; id 对象;或 id some_object = NULL; while(obj = [theEnum nextObject]) { //do
some_object = NULL;/ id obj; 我想如果我能更好地理解什么代表我可以自己解决。
在我的代码中我有三个数组。我希望每次用户单击“下一步”按钮时都能够在 UILabel 字段中的每个数组中显示一个对象,直到所有对象都显示出来。
NSArray1 = 1,2,3
NSArray2 = 约翰、吉尔、乔什 NSArray3 = 男孩,女孩,男孩
当按下下一个按钮时,您将看到 1、约翰和男孩。下次你会看到 2,吉尔和女孩,最后看到 3,乔希和男孩。
下面是基本示例,不是我的实际代码。
number = [[NSArray alloc] initWithObjects:@"1",@"2",@"3", nil];
name = [[NSArray alloc] initWithObjects:@"John",@"Jill",@"Josh", nil];
gender = [[NSArray alloc] initWithObjects:@"boy",@"girl",@"boy", nil];
NSEnumerator *enum = [number objectEnumerator];
id obj; (??What is this?? And how to connect to the statement below??)
while ((obj = [enumNumber nextObject])) {
self.numberItem.text = ??
self.nameItem.text = ??
self.genderItem.text = ??
谢谢
I am new to Objective-C and I have researched this online for weeks now. Almost every example is the same on every site and does not fully make it clear to me how to integrate it into my code for an Xcode 4 App.
The example seen everywhere is:
NSEnumerator* theEnum = [some_array objectEnumerator];
id obj; or id some_object = NULL;
while(obj = [theEnum nextObject]) {
//do something...
I think if I better understood what id some_object = NULL;/ id obj; represents I could figure it out on my own.
In my code I have three arrays. I want to be able to display one object in each array in a UILabel field every time the user clicks the Next button until all of them have been displayed.
NSArray1 = 1,2,3
NSArray2 = John, Jill, Josh
NSArray3 = boy, girl, boy
When the next button is pushed you would see 1, John and boy. The next time you would see 2, Jill and girl and finally 3, Josh and boy.
Below is basic example, not my actually code.
number = [[NSArray alloc] initWithObjects:@"1",@"2",@"3", nil];
name = [[NSArray alloc] initWithObjects:@"John",@"Jill",@"Josh", nil];
gender = [[NSArray alloc] initWithObjects:@"boy",@"girl",@"boy", nil];
NSEnumerator *enum = [number objectEnumerator];
id obj; (??What is this?? And how to connect to the statement below??)
while ((obj = [enumNumber nextObject])) {
self.numberItem.text = ??
self.nameItem.text = ??
self.genderItem.text = ??
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
id obj
只是包含您正在查看的当前变量的变量。在此代码中,由于您的NSArray
都包含字符串,因此您可以使用NSString *obj
代替。枚举器一次只能枚举一个集合。如果您想一起循环遍历所有三个,请使用传统的
for
循环:id obj
is just the variable containing the current variable you're looking at. In this code, since yourNSArray
s all contain string, you could useNSString *obj
instead.An enumerator can only enumerate one collection at a time. If you want to loop through all three of them together, use a traditional
for
loop: