OR and AND Syntax in If condition
I want to save all sound files in one array 开发者_如何学Clike .caf, .avi ,.mp3.. How can I check OR condition for this.I am confused about syntax.
#define kSoundFileExtension @"caf"
#define kSoundfileExtnesion @"mp3"
if ([fileName rangeOfString:kSoundFileExtension].location != NSNotFound) {
[self.soundFiles addObject:fileName];
}
You would need to give each extension a different identifier, and then check for both, like this:
#define kSoundFileCAFExtension @"caf"
#define kSoundFileMP3Extension @"mp3"
if (([fileName rangeOfString:kSoundFileCAFExtension].location != NSNotFound) ||
([fileName rangeOfString:kSoundFileMP3Extension].location != NSNotFound)) {
[self.soundFiles addObject:fileName];
}
NSArray *exts = [NSArray arrayWithObjects:@"caf",@"mp3",nil];
for(NSString *ext in exts) {
if ([[fileName pathExtension] isEqualToString:ext]) {
[self.soundFiles addObject:fileName];
break;
}
}
This is for OR :
if (([fileName rangeOfString:kSoundFileCAFExtension].location != NSNotFound) ||
([fileName rangeOfString:kSoundFileMP3Extension].location != NSNotFound)) {
This is for AND :
if (([fileName rangeOfString:kSoundFileCAFExtension].location != NSNotFound) &&
([fileName rangeOfString:kSoundFileMP3Extension].location != NSNotFound)) {
In first case either of two needs to fulfill the condition and in second case both need to fulfill the condition.
精彩评论