Comparing a string with an array in objective-C

here is a very simple question that I'm sure you can answer quickly. Please do not laugh about my ignorance.

I have a string that I want to compare with an array of strings. Only if the string is not part of the array, I want to perform the operation. I tried the following code that does not work. I understand why, but I just can’t figure out how to do it right.

Please help me in my suffering.

Thank you in advance

Sjakelien

-(void) findRedundant: (NSString *) aString { #define ALPHA_ARRAY [NSArray arrayWithObjects: @"A", @"B", @"C", nil] NSUInteger f; for (f = 0; f < [ALPHA_ARRAY count]; f++) { NSString * stringFromArray = [ALPHA_ARRAY objectAtIndex:f]; if ([aString isEqualToString:stringFromArray]) { // do nothing } else { //do something } } } [self findRedundant:@"D"]; 
+4
source share
3 answers

Your code is working fine. Its awful code, but it works fine, // nothing is called for any section, and // calls something section for every mismatch in the array. I suspect the problem is that you expect that // there will be nothing section that will be executed once if there is no match, and // do something section to execute once if there is any match , what's wrong. You probably want:

 -(void) findRedundant: (NSString *) aString { #define ALPHA_ARRAY [NSArray arrayWithObjects: @"A", @"B", @"C", nil] BOOL found = NO; NSUInteger f; for (f = 0; f < [ALPHA_ARRAY count]; f++) { NSString * stringFromArray = [ALPHA_ARRAY objectAtIndex:f]; if ([aString isEqualToString:stringFromArray]) { found = YES; break; } } if ( found ) { // do found } else { // do not found } } 

In addition, you clearly do not understand macros, and when you should and should not use them (as a rule, you should never use them, with very few exceptions). The macro in the text expression has been replaced with your code. This means that the creation and initialization of the array occurs every time , you use ALPHA_ARRAY. It's horrible.

Basically, never use #define again (except for constants) until you get a much deeper understanding of what you are doing. In this case, you will create an array as described by taebot:

 NSArray* alphaArray = [NSArray arrayWithObjects: @"A", @"B", @"C", nil]; 

Further, if you are developing a modern platform (10.5 or iPhone), you can use Fast Enumeration, which is much easier and more understandable to read:

 -(void) findRedundant: (NSString *) aString { NSArray* alphaArray = [NSArray arrayWithObjects: @"A", @"B", @"C", nil]; BOOL found = NO; for ( NSString* stringFromArray in alphaArray ) { if ([aString isEqualToString:stringFromArray]) { found = YES; break; } } if ( found ) { // do found } else { // do not found } } 

And finally, you should read the NSArray and NSString documentation to find out what you can do for free, and then you will find methods like containsObject that KiwiBastard pointed out, and you can rewrite your procedure as:

 -(void) findRedundant: (NSString *) aString { NSArray* alphaArray = [NSArray arrayWithObjects: @"A", @"B", @"C", nil]; if ( [alphaArray containsObject: aString] ) { // do found } else { // do not found } } 
+12
source

I'm not sure why your code above does not work, but you tried:

 if ([ALPHA_ARRAY containsObject:aString]) 

which will return YES if aString exists otherwise NO

+7
source

#define looks weird to me. I think that every time you use ALPHA_ARRAY, another instance of NSArray will be created. It would be cleaner to use the containsObject: method on an NSArray:

 NSArray* alphaArray = [NSArray arrayWithObjects: @"A", @"B", @"C", nil]; if (![alphaArray containsObject:aString]) { // do something } 
+6
source

Source: https://habr.com/ru/post/1286410/


All Articles