Loop!!!!

Discussion in 'iOS Programming' started by kurkah, Apr 22, 2010.

  1. kurkah macrumors newbie

    Joined:
    Apr 22, 2010
    Location:
    U.K.
    #1
    Hi,

    just working on a word checker app, have the following code:

    Code:
    NSArray* myarray_;
    NSString* return_=@"";
    myarray_ = [[NSArray alloc] initWithObjects:@"one",@"two",@"three",@"four",nil];
    .
    .
    .
    for (int x=0;x<=sizeof myarray_;x++)
    {
    NSString* wordcheck_=[NSString stringWithFormat:@"%@",[myarray_ objectAtIndex:x]];
    		
    if ([inputWORD_ isEqualToString: wordcheck_])
    		return_=@"YES";
    	else 
    		return_=@"No";
    }
    	return return_;
    .
    .
    .


    the problem i am having is whenever i insert the last element (i.e. "four"), it seems to be alright, HOWEVER, when i try the rest elements it is not working.... it returns no.

    I am new to objective-c, this code would simply work on java, but i dont know why its not working

    So i would appreciate it if someone there could give me help.:apple:

    thanks!
     
  2. fishkorp macrumors 68020

    fishkorp

    Joined:
    Apr 10, 2006
    Location:
    Ellicott City, MD
    #2
    You're constantly overwriting your _return variable, so it'll only return YES if the word matches the last item in the array. You need to just quit the loop once you find a match. And no, that code would not work in Java either, or any other language. No matter what language it was, you'd be overwriting the variable every time and only hanging on to the last one.
     
  3. kurkah thread starter macrumors newbie

    Joined:
    Apr 22, 2010
    Location:
    U.K.
    #3
    I think I hv got what you mean but I still cant see it code wise???
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    The use of <= is completely wrong. Use < instead.

    The use of sizeof is also wrong. Use [myarray_ count] instead.

    The only reason sizeof seems to work is that it just happens to be the same as the array's length: 4. The sizeof expression is 4 because a 32-bit pointer has a size of 4 (32-bits is 4 bytes).
     
  5. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #5
    Further to the above, you should take advantage of NSFastEnumeration whenever you can; cleaner syntax and less prone to errors as pointed out above:

    Code:
    for(NSString *string in myArray) {
      // do something
    }
    
    May I also add that using a variable named 'return_' is makes the code very hard to scan as on glance it looks like a return keyword.
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Find the command that causes a loop to stop running.
     
  7. kurkah thread starter macrumors newbie

    Joined:
    Apr 22, 2010
    Location:
    U.K.
    #7
    Thanks a lot guysss.

    I used the first suggestion,
    .
    .
    .

    Code:
    for (int x=0;x<[myarray count];x++)
    {
    check=[NSString stringWithFormat:@"%@",[arry objectAtIndex:x]];
    	if ([s1 isEqualToString:check])
    		return_=@"YES";
    	else
    		return_=@"No";
    }
    	return return_;
    .
    .
    .
    And even the second one...


    Code:
    for(NSString* string in myarray)
    {
    	if ([s1 isEqualToString:string])
    		return_=@"YES";
    	else
    		return_=@"No";
    }
    	return return_;
    .
    .
    .

    BUT, i still have the same problem, only the last element is checked.????
     
  8. fishkorp macrumors 68020

    fishkorp

    Joined:
    Apr 10, 2006
    Location:
    Ellicott City, MD
    #8
    You just need to tell it to quit once you find a match. If you find a yes, immediately return a yes, otherwise, return a no after the loop finishes.
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    Or if you prefer a single point of return, find a way to break out of the loop as it's running.

    P.S. There's a hint in what I said.
     
  10. kurkah thread starter macrumors newbie

    Joined:
    Apr 22, 2010
    Location:
    U.K.
    #10


    Thanks guys i think the problem was the experience of porgramming....

    Code:
    for(NSString* string in myarray)
    {
    	if ([s1 isEqualToString:string])
    	{	
    		return_=@"YES";
    		break;
    	}
    	else
    		return_=@"No";
    }
    	return return_;

    the break allowing the loop to terminate once the selection is fulfilled.

    Its working now
    SO
    thanks
     
  11. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000

Share This Page