开发者

Unable to get NSEnumerator to display next item in my array

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 开发者_JAVA技巧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


id obj is just the variable containing the current variable you're looking at. In this code, since your NSArrays all contain string, you could use NSString *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:

for(unsigned int i = 0; i < number.count; i++) {
    self.numberItem.text = [number objectAtIndex:i];
    self.nameItem.text = [name objectAtIndex:i];
    self.genderItem.text = [gender objectAtIndex:i];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜