Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

kurkah

macrumors newbie
Original poster
Apr 22, 2010
10
0
U.K.
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!
 

fishkorp

macrumors 68030
Apr 10, 2006
2,536
650
Ellicott City, MD
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.
 

kurkah

macrumors newbie
Original poster
Apr 22, 2010
10
0
U.K.
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.

I think I hv got what you mean but I still cant see it code wise???
 

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,421
A sea of green
Code:
NSArray* myarray_;
NSString* return_=@"";
myarray_ = [[NSArray alloc] initWithObjects:@"one",@"two",@"three",@"four",nil];
.
.
.
for (int x=0;[COLOR="Red"]x<=sizeof myarray_[/COLOR];x++)
...

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).
 

Luke Redpath

macrumors 6502a
Nov 9, 2007
733
6
Colchester, UK
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.
 

kurkah

macrumors newbie
Original poster
Apr 22, 2010
10
0
U.K.
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.????
 

fishkorp

macrumors 68030
Apr 10, 2006
2,536
650
Ellicott City, MD
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.
 

kurkah

macrumors newbie
Original poster
Apr 22, 2010
10
0
U.K.
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.



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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.