iOS 位移枚举NS_OPTIONS(如何实现多个枚举值的同时传入判断)

一、场景

当我们使用枚举这个东西时,有时需要多个枚举值任一一个满足时就ture,但是常用的枚举NS_ENUM定义好的枚举只能挨个判断,写一坨就既不美观也不好阅读,如下:

typedef NS_ENUM (NSInteger, RPTestType){
    RPTestTypeUnknown = 0, // unknow
    RPTestTypeValue0,      // 值0
    RPTestTypeValue1,      // 值1
    RPTestTypeValue2,      // 值2
    RPTestTypeValue3,      // 值3
    RPTestTypeValue4,      // 值4
    RPTestTypeValue5,      // 值5
};

RPTestType testTeype = RPTestTypeUnknown;
if (testTeype == RPTestTypeValue0 ||
    testTeype == RPTestTypeValue1 || 
    testTeype == RPTestTypeValue2 || 
    testTeype == RPTestTypeValue3 || 
    testTeype == RPTestTypeValue4 || 
    testTeype == RPTestTypeValue5) {
    NSLog(@"ture");
}

二、利用NS_OPTIONS优化

1、先了解一个关于位运算符的知识点:位运算符浅析

这里用到(左移 << )和 (按位与 & )和(按位或 | )这仨,先明确是怎么个事;

2、先看下NS_OPTIONS如何来定义枚举
typedef NS_OPTIONS (NSInteger, RPTestType){
    RPTestTypeUnknown = 1 << 0, // unknow
    RPTestTypeValue0  = 1 << 1, // 值0
    RPTestTypeValue1  = 1 << 2, // 值1
    RPTestTypeValue2  = 1 << 3, // 值2
    RPTestTypeValue3  = 1 << 4, // 值3
    RPTestTypeValue4  = 1 << 5, // 值4
    RPTestTypeValue5  = 1 << 6, // 值5
};

这里使用左移定义枚举值,结合位运算可以这么看:
1 << 0 就是 0001,即2的0次方;
1 << 1 就是 0010,即2的1次方;
1 << 2 就是 0100,即2的2次方;
以此类推~

3、再看这么用的好处

当有多个枚举值都符合时做判断时候,可以直接(或 | )起来,然后(与 & )一下子,就能清晰明了的做判断;

RPTestType testType = RPTestTypeUnknown;
RPTestType judgeType = RPTestTypeValue0 | RPTestTypeValue1 | RPTestTypeValue2 | RPTestTypeValue3 | RPTestTypeValue4 | RPTestTypeValue5;
if (testType & judgeType) {
    NSLog(@"ture");
}

当然最好使的用处是枚举值传参,如SDImage

[self.testImgView sd_setImageWithURL:[NSURL URLWithString:@"xx"]
                    placeholderImage:nil
                             options:SDWebImageLowPriority | 
                                     SDWebImageProgressiveLoad |
                                     SDWebImageRefreshCached |
                                     SDWebImageContinueInBackground];

options这里可以直接传入多个枚举值,可用于方法内部与或运算进行判断,方便传值;类似的还有UIControlState;

typedef NS_OPTIONS(NSUInteger, UIControlState) {
    UIControlStateNormal       = 0,
    UIControlStateHighlighted  = 1 << 0,                  // used when UIControl isHighlighted is set
    UIControlStateDisabled     = 1 << 1,
    UIControlStateSelected     = 1 << 2,                  // flag usable by app (see below)
    UIControlStateFocused API_AVAILABLE(ios(9.0)) = 1 << 3, // Applicable only when the screen supports focus
    UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use
    UIControlStateReserved     = 0xFF000000               // flags reserved for internal framework use
};
[testBtn setTitle:@"xx"
         forState:UIControlStateNormal | 
                  UIControlStateHighlighted | 
                  UIControlStateSelected];
  • 16
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS 上,你可以使用 AVAudioRecorder 和 AVAudioPlayer 来同时进行录音和播放。 首先,你需要创建一个 AVAudioRecorder 实例来录制音频。然后,你可以在录制音频的同时,创建一个 AVAudioPlayer 实例来播放音频。在录制和播放过程中,你需要将音频数据写入文件和缓存中。 以下是一个示例代码,展示了如何同时录制和播放音频: ```swift import AVFoundation class AudioController: NSObject, AVAudioRecorderDelegate { var recorder: AVAudioRecorder? var player: AVAudioPlayer? func startRecordingAndPlayback() { // 设置录音会话 let session = AVAudioSession.sharedInstance() try? session.setCategory(AVAudioSession.Category.playAndRecord, mode: .default, options: []) try? session.setActive(true) // 设置录音器 let settings: [String: Any] = [AVFormatIDKey: Int(kAudioFormatMPEG4AAC), AVSampleRateKey: 44100.0, AVNumberOfChannelsKey: 2, AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue] let url = URL(fileURLWithPath: NSTemporaryDirectory() + "recording.m4a") recorder = try? AVAudioRecorder(url: url, settings: settings) recorder?.delegate = self recorder?.prepareToRecord() recorder?.record() // 设置播放器 let audioData = try? Data(contentsOf: url) player = try? AVAudioPlayer(data: audioData ?? Data()) player?.prepareToPlay() player?.play() } // AVAudioRecorderDelegate 方法 func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) { player?.stop() } } ``` 在这个示例中,我们首先设置了一个音频会话,然后创建了一个 AVAudioRecorder 实例来录制音频。同时,我们也创建了一个 AVAudioPlayer 实例来播放音频。最后,在录音完成后,我们停止了播放器。 你可以根据自己的需求进行更改和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值