标签: rawrepresentable

Swift枚举继承

你能在Swift中继承enum吗?关于枚举继承,应该注意哪些规则?

以下测试代码:

enum TemperatureUnit: Int {
    case Kelvin, Celcius, Farenheit
}

enum TemperatureSubunit : Temperature {  
}
Run Code Online (Sandbox Code Playgroud)

生成

error: type 'TemperatureSubunit' does not conform to protocol 'RawRepresentable'
Run Code Online (Sandbox Code Playgroud)

inheritance enums ios swift rawrepresentable

53
推荐指数
3
解决办法
3万
查看次数

在Swift中,如何指定将RawRepresentable限制为String的类型?

我正在尝试定义一个需要实现enum原始值的协议String.

我不相信它现在可以强制使用enum,而且我不确定我是否真的在乎,只要我可以打电话fromRaw()和接收一个地方String.

所以,我试图保持以下的简洁性,同时限制Beta是一个enum地方的原始值是String:

protocol Alpha {
    typealias Beta: RawRepresentable
}

struct Gamma: Alpha {
    enum Beta: String {
        case Delta = "delta"
    }
}

struct Eta<T: Alpha, U: RawRepresentable where T.Beta == U> {
    let alpha: T
    let beta: U
    init(alpha: T, beta: U) {
        self.alpha = alpha
        self.beta = beta
        println("beta is: \(beta.toRaw())")
    }
}

let gamma = Gamma()
Eta(alpha: gamma, …
Run Code Online (Sandbox Code Playgroud)

swift rawrepresentable

12
推荐指数
2
解决办法
4813
查看次数

类型'错误'不符合协议'RawRepresentable'

将我的游乐场代码更改为Swift 3,Xcode建议更改

enum Error: ErrorType {
    case NotFound
}
Run Code Online (Sandbox Code Playgroud)

enum Error: Error {
    case NotFound
}
Run Code Online (Sandbox Code Playgroud)

但现在我得到标题错误,我不知道如何使枚举符合该协议.

swift rawrepresentable

10
推荐指数
1
解决办法
8466
查看次数

Swift 3.0中结构的枚举

我正在尝试创建一个我想初始化的结构的枚举:

struct CustomStruct {
    var variable1: String
    var variable2: AnyClass
    var variable3: Int

    init (variable1: String, variable2: AnyClass, variable3: Int) {
        self.variable1 = variable1
        self.variable2 = variable2
        self.variable3 = variable3
    }
}

enum AllStructs: CustomStruct {
    case getData
    case addNewData

    func getAPI() -> CustomStruct {
        switch self {
            case getData:
                return CustomStruct(variable1:"data1", variable2: SomeObject.class, variable3: POST)

            case addNewData:
                // Same to same

            default:
                return nil
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

Type AllStructs不符合协议'RawRepresentable'

我假设枚举不能以这种方式使用.我们必须使用原语.

enums ios swift rawrepresentable

10
推荐指数
3
解决办法
6611
查看次数

原始类型'Bool'不能通过任何文字表达

我想让我的枚举容易兼容@IBInspectable,所以为了简单起见,我试着用类型表示它Bool:

enum TopBarStyle: Bool {
    case darkOnLight
    case lightOnDark
}
Run Code Online (Sandbox Code Playgroud)

但是Xcode给了我:

原始类型'Bool'不能通过任何文字表达

这很奇怪,因为true并且false似乎是文字表达的完美候选人.

我还尝试将RawRepresentable类型添加到Bool类型:

extension Bool: RawRepresentable {
    public init?(rawValue: Bool) {
        self = rawValue
    }
    public var rawValue: Bool {
        get { return self }
    }
}
Run Code Online (Sandbox Code Playgroud)

但它没有解决错误.

enums swift rawrepresentable

7
推荐指数
4
解决办法
5346
查看次数

为任意的、基于整数的枚举定义 Swift 协议

我有这个代表颜色的枚举,并且我添加了几种方法来方便地获取基于原始原始值的算术运算的新实例:

enum Color : Int
{
    case Red = 0
    case Green
    case Blue

    case Cyan
    case Magenta
    case Yellow


    static func random() -> Color
    {
        return Color(rawValue: Int(arc4random_uniform(6)))!
    }

    func shifted(by offset:Int) -> Color
    {
        return Color(rawValue: (self.rawValue + offset) % 6)!
        // Cyclic: wraps around
    }
}
Run Code Online (Sandbox Code Playgroud)

(这可以追溯到旧的枚举只是 int 常量)

问题是,我还有其他几个基于 int 的枚举,我想在其中引入类似的功能,但不重复代码。

我想我应该在RawRepresentablewhere上定义一个协议扩展RawValue == Int

extension RawRepresentable where RawValue == Int
{
Run Code Online (Sandbox Code Playgroud)

...但这就是我对语法的理解结束的地方。

理想情况下,我想要一个返回案例数量的静态方法,并提供一个考虑到这一点的两个random()shifted(_:)以上的默认实现(而不是这里的硬编码 6)。

结论:我接受了 Zoff …

enums protocols swift rawrepresentable

6
推荐指数
1
解决办法
1391
查看次数

如何制定描述字符串表示的枚举的协议?

我有一个方法,它调用某个管理器的方法来用某个键保存 int 值。我的方法接收 int 和一些 EnumKey 枚举值作为键,挤出 EnumKey 的 rawValue 并将其作为字符串传递给管理器:

set(value: Int, forKey key: EnumKey) {
    SomeManager.saveIntValueWithStringKey(valueToSave: value, keyToSave: key.rawValue)
}

enum EnumKey: String { 
    case One="first key"
    case Two="second key"
}
Run Code Online (Sandbox Code Playgroud)

我想通过允许我的方法接收带有字符串原始值而不是 EnumKey 的每个枚举来使其更加通用。在方法的实现中,我将关键参数的类型从 EnumKey 替换为 GenericKey 协议,并使 EnumKey 符合此协议:

 set(value: Int, forKey key: GenericKey) {
    SomeManager.saveIntValueWithStringKey(valueToSave: value, keyToSave: key.rawValue)
}

protocol GenericKey {
    var rawValue: String { get }
}

enum EnumKey: String, GenericKey { 
    case One="first key"
    case Two="second key"
}
Run Code Online (Sandbox Code Playgroud)

但这String, GenericKey看起来有点难看。我希望每个可表示字符串的枚举都能自动适应,而不需要提及它除了 RawRepresentable …

generics enums swift swift-protocols rawrepresentable

4
推荐指数
1
解决办法
2499
查看次数

如何在Swift中创建泛型枚举的实例

我正在尝试在swift中编写一个函数,在这样的泛型函数中创建一个rawValue枚举:

enum STATE: String {
    case OK = "OK"
    case ERROR = "ERROR"
}

func createEnum<E: RawRepresentable>(rawValue: T.Type) {
    return E(rawValue: rawValue) // compiler error 
}
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?

generics enums swift rawrepresentable

2
推荐指数
1
解决办法
1644
查看次数

如何使用组合类型 CustomStringConvertible 和 RawRepresentable 指定函数参数的类型?

enum我想要一个通用函数,它可以通过提供枚举类型和Int原始值来实例化我拥有的几种不同类型的对象。这些enum也是CustomStringConvertible

我试过这个:

func myFunc(type: CustomStringConvertible.Type & RawRepresentable.Type, rawValue: Int)
Run Code Online (Sandbox Code Playgroud)

这会导致 3 个错误:

  • 非协议、非类类型“CustomStringConvertible.Type”不能在协议约束类型中使用
  • 非协议、非类类型“RawRepresentable.Type”不能在协议约束类型中使用
  • 协议“RawRepresentable”只能用作通用约束,因为它具有 Self 或关联的类型要求

现在忘记“CustomStringConvertible”,我也尝试过:

private func myFunc<T: RawRepresentable>(rawValue: Int, skipList: [T]) {
    let thing = T.init(rawValue: rawValue)
}
Run Code Online (Sandbox Code Playgroud)

但是,尽管代码完成建议这样做,但会导致以下错误T.init(rawValue:)

  • 无法使用类型为“(rawValue: Int)”的参数列表调用“init”

我怎样才能形成这样一个有效的通用函数?

generics ios swift rawrepresentable

1
推荐指数
1
解决办法
705
查看次数