swift 3.0中的结构体枚举 [英] enum of structs in swift 3.0

查看:111
本文介绍了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
}
}

枚举AllStructs:CustomStruct {
case getData
case addNewData

func getAPI() - > CustomStruct {
switch self {
case getData:
return CustomStruct(variable1:data1,variable2:SomeObject.class,variable3:POST)
case addNewData:
//同样的
默认值:
返回nil
}
}
}

我收到以下错误


类型AllStructs不符合协议'RawRepresentable' p>

我假设枚举不能这样使用?我们必须使用原语。

解决方案

应该是:

  struct CustomStruct {
var apiUrl:String
var responseType:AnyObject
var httpType:Int

init(variable1:String,variable2 :AnyObject,variable3:Int){
self.apiUrl = variable1
self.responseType = variable2
self.httpType = variable3
}
}

enum MyEnum {
case getData
case addNewData

func getAPI() - > CustomStruct {
switch self {
case .getData:
return CustomStruct(variable1:URL_TO_GET_DATA,variable2:11 as AnyObject,variable3:101)
case .addNewData:
return CustomStruct(variable1:URL_TO_ADD_NEW_DATA,variable2:12 as AnyObject,variable3:102)
}
}
}

用法:

  let data = MyEnum.getData 
let myObject = data.getAPI()

//应该记录:URL_TO_GET_DATA 11 101
print(myObject.apiUrl,myObject.responseType,myObject.httpType)
请注意,在命名规则中,struct应该命名为 CustomStruct 和枚举名为 MyEnum



其实我不太确定需要让$ code> CustomStruct 成为 MyEnum 的父级,以实现您想要的内容;如上所述,在代码片段中,您可以根据引用枚举的值返回结构体的实例。



希望这有帮助。


I am trying to create a enum of a struct that I would like to initialize

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
    }
}
}

I get the following errors

Type AllStructs does not conform to protocol 'RawRepresentable'

I am assuming that enums cannot be used this way ? We must use primitives.

解决方案

It should be:

struct CustomStruct {
    var apiUrl: String
    var responseType: AnyObject
    var httpType: Int

    init (variable1: String, variable2: AnyObject, variable3: Int) {
        self.apiUrl = variable1
        self.responseType = variable2
        self.httpType = variable3
    }
}

enum MyEnum {
    case getData
    case addNewData

    func getAPI() -> CustomStruct {
        switch self {
        case .getData:
            return CustomStruct(variable1: "URL_TO_GET_DATA", variable2: 11 as AnyObject, variable3: 101)
        case .addNewData:
            return CustomStruct(variable1: "URL_TO_ADD_NEW_DATA", variable2: 12 as AnyObject, variable3: 102)
        }
    }
}

Usage:

let data = MyEnum.getData
let myObject = data.getAPI()

// this should logs: "URL_TO_GET_DATA 11 101"
print(myObject.apiUrl, myObject.responseType, myObject.httpType)

Note that upon Naming Conventions, struct should named as CustomStruct and enum named as MyEnum.

In fact, I'm not pretty sure of the need of letting CustomStruct to be the parent of MyEnum to achieve what are you trying to; As mentioned above in the snippets, you can return an instance of the struct based on what is the value of the referred enum.

Hope this helped.

这篇关于swift 3.0中的结构体枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆