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

rbert

macrumors newbie
Original poster
Nov 15, 2009
12
0
I have a myString that i break into an array of words.
My goal is to determinate if the content of the myString mets the conditions:
- if myString contains all of "0", it prints out "Failed!"
- if myString contains all of "1", it prints out "Passed!"
- if myString contains a mixture of "1" and "0", it prints out "Partially Passed!"

Here is the code i try but it does not work in the while loop
Can you please help me, thanks!

PHP:
	NSString *myString = @"1 1 1 2 0 1 2 0 1 0";
	
	//break NSString *myString into an NSArray containing 
	//the individual words separated by whitespace, newlines, and punctuation
	NSMutableCharacterSet *separators = [NSMutableCharacterSet punctuationCharacterSet];
	[separators formUnionWithCharacterSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
	NSArray *words = [myString componentsSeparatedByCharactersInSet:separators];

	NSEnumerator *enumerator;
	enumerator = [words objectEnumerator];
	NSString *stringContents;
	
	NSString *result;

	//cycles through array
        //It does not work up to here:
	while (stringContents = [enumerator nextObject]) {
	
		if ([stringContents isEqualToString:@"0"]) {
result=@"Failed!";
		}
		
		if ([stringContents isEqualToString:@"1"]) {
result=@"Passed!";
		}
		
		if ([stringContents isEqualToString:@"0"] && [stringContents isEqualToString:@"1"]) {
result=@"Partially Passed!";
		}
		
	}
	
	//print out result either failed, passed or partially passed.
	//but it does not work with the output outside the loop.
	NSLog(@"%@", result);
 

kpua

macrumors 6502
Jul 25, 2006
294
0
How about this:

Code:
BOOL hasZero = [myString rangeOfString:@"0"].location != NSNotFound;
BOOL hasOne = [myString rangeOfString:@"1"].location != NSNotFound;

if (hasZero && hasOne) {
    result=@"Partially Passed!";
} else if (hasZero) {
    result=@"Failed!";
} else if (hasOne) {
    result=@"Passed!";
}
 

rbert

macrumors newbie
Original poster
Nov 15, 2009
12
0
Thank You!

omg :eek: it's soo short and exactly what i am looking for.
Thank you kpua for the very elegant solution!!
 

rbert

macrumors newbie
Original poster
Nov 15, 2009
12
0
Thanks lee, i added the else condition for not defined.
I have another question, is there a way to defined the string range for BOOL hasOne from 1 to 100 instead of using a fixed @"1", so that it match more. Something like this:
Thank you for your help!

PHP:
NSString *myString = @"1 1 1 2 0 1 2 0 1 0";

BOOL hasZero = [myString rangeOfString:@"0"].location != NSNotFound;
//BOOL hasOne = [myString rangeOfString:@"1"].location != NSNotFound;
BOOL hasOne = [myString rangeOfString:{1-100}].location != NSNotFound;

if (hasZero && hasOne) {
    result=@"Partially Passed!";
} else if (hasZero) {
    result=@"Failed!";
} else if (hasOne) {
    result=@"Passed!";
} else {
    result=@"Not defined!";
}

NSLog(@"%@", result);
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
explain yourself more clearly.

Do you only want to check if there is a 1 in the first 100 characters of myString, or do you want to see if the string representation of the numbers 1 - 100 appear? If the latter, 0 appears in 10, 20, 30, etc., so are you looking for these values as "words" in the string?

If you're looking for words, you can do something like this:
Code:
NSString *myString = @"1 1 1 2 0 1 2 0 1 0";
BOOL hasZero = NO;
BOOL hasNonZero = NO;
BOOL hasInvalid = NO;
NSArray splitString = [myString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
for(NSString singleString in splitString) {
  if([singleString isEqualToString:@"0"]) {
    hasZero = YES;
  } else {
    int intRep = [singleString intValue];
    if(intRep > 0 && intRep <= 100) {
      hasNonZero = YES;
    } else {
      hasInvalid = YES;
    }
  }
  if(hasZero && hasNonZero && hasInvalid) break; //Nothing else to see
}

if (hasZero && hasNonZero) {
    result=@"Partially Passed!";
} else if (hasZero) {
    result=@"Failed!";
} else if (hasNonZero) {
    result=@"Passed!";
} else if(hasInvalid) {
    result=@"Not defined!";
}

NSLog(@"%@", result);

I haven't compiled this, so it may not work 100% as is, but the idea should be clear.

-Lee
 

rbert

macrumors newbie
Original poster
Nov 15, 2009
12
0
Unbelievable, you code it with pure brain without testing and very quick! :eek:
Yes i am looking for these values as "words" in the string and your code is full working, i just admire you!
Thank you very much lee!!!

I have tested it and it is working:
PHP:
	/* Code written by lee1210 @ macrumors.com */

	//NSString *myString = @"1 2 3 40 50 60 70"; // Out: Passed! > Correct
	//NSString *myString = @"0 0 0 0 0 0 0"; // Out: Failed! > Correct
	NSString *myString = @"1 2 0 40 50 60 70"; // Out: Partially Passed! > Correct

	BOOL hasZero = NO;
	BOOL hasNonZero = NO;
	BOOL hasInvalid = NO;
	NSString *result;
	
    //break NSString *myString into an NSArray containing 
    //the individual words separated by whitespace, newlines, and punctuation
    NSMutableCharacterSet *separators = [NSMutableCharacterSet punctuationCharacterSet];
    [separators formUnionWithCharacterSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSArray *splitString = [myString componentsSeparatedByCharactersInSet:separators];

    //cycles through array
	for(NSString *singleString in splitString) {
		if([singleString isEqualToString:@"0"]) {
			hasZero = YES;
		} else {
			int intRep = [singleString intValue];
			if(intRep > 0 && intRep <= 100) {
				hasNonZero = YES;
			} else {
				hasInvalid = YES;
			}
		}
		if(hasZero && hasNonZero && hasInvalid) break; //Nothing else to see
	}
	
    //print result
	if (hasZero && hasNonZero) {
		result=@"Partially Passed!";
	} else if (hasZero) {
		result=@"Failed!";
	} else if (hasNonZero) {
		result=@"Passed!";
	} else if(hasInvalid) {
		result=@"Not defined!";
	}
	
	NSLog(@"%@", result);
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.