开发者

OR and AND Syntax in If condition

开发者 https://www.devze.com 2023-03-08 10:36 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消