swift 3.0中的结构体枚举 [英] enum of structs in 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 asMyEnum
.In fact, I'm not pretty sure of the need of letting
CustomStruct
to be the parent ofMyEnum
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屋!