如何将枚举限制为仅采用协议的人员,而协议又取决于该枚举?

时间:2018-09-04 07:31:28

标签: swift enums protocols swift-protocols

我想做的是将与协议相同的文件中声明的Taptic枚举的可见性限制为仅采用该协议的那些文件。现在,任何文件都可以“查看”枚举。我尝试了将枚举或案例的访问修饰符更改为fileprivate的组合,但是不允许使用它们。我也尝试过将枚举声明为enum Taptic: UInt 32 where Self: Vibratable' {,但这不是正确的语法。有指针吗?

import UIKit
import AudioToolbox

enum Taptic: UInt32 {
    case Peek = 1519
    case Pop = 1520
    case Nope = 1521
}

protocol Vibratable {
    var tapticLevel: Int { get }
    func vibrateDevice(forHigherThan6s: UIImpactFeedbackStyle, for6s: Taptic, lowerThan6s: SystemSoundID)
}

extension Vibratable {
    var tapticLevel: Int {
        return UIDevice.current.value(forKey: "_feedbackSupportLevel") as! Int
    }
    func vibrateDevice(forHigherThan6s: UIImpactFeedbackStyle = .medium, for6s: Taptic = .Peek, lowerThan6s: SystemSoundID = kSystemSoundID_Vibrate) {
        switch tapticLevel {
        case 2:
            let generator = UIImpactFeedbackGenerator(style: forHigherThan6s)
            generator.prepare()
            generator.impactOccurred()
        case 1:
            AudioServicesPlaySystemSound(for6s.rawValue)
        default:
            AudioServicesPlaySystemSound(lowerThan6s)
        }
    }
}

0 个答案:

没有答案
相关问题