I'm new to Swift iOS.. I'm trying to use enum type as input parameter to a function.. here is the
我是Swift iOS的新手。我正在尝试使用枚举类型作为函数的输入参数..这里是
sample enum type:
样本枚举类型:
public enum CFScreen {
public enum CFScreen_Newsfeed: Int {
case Newsfeed_01 = 2100
case Newsfeed_02 = 2101
case Newsfeed_03 = 2102
case Newsfeed_04 = 2103
case Newsfeed_05 = 2104
case Newsfeed_06 = 2105
case Newsfeed_07 = 2106
case Newsfeed_08 = 2107
}
public enum CFScreen_Groups: Int {
case Create = 2200
case GoBack = 2201
}
}
sample function using enum type as input parameter
使用枚举类型作为输入参数的示例函数
func showCFScreenForEnumType(cFooter: CFScreen.CFScreen_Groups) {
//Group Related
switch cFooter {
case .Create:
print("Show Create Group")
break
case .GoBack:
print("Show Go Back Screen")
break
}
}
func showCFScreenForEnumType(cFooter: CFScreen.CFScreen_Newsfeed) {
//Group Related
switch cFooter {
case .Newsfeed_01:
print("Show News 01")
break
case .Newsfeed_02:
print("Show News 02")
break
case .Newsfeed_03:
print("Show News 03")
break
case .Newsfeed_04:
print("Show News 04")
break
case .Newsfeed_05:
print("Show News 05")
break
case .Newsfeed_06:
print("Show News 06")
break
case .Newsfeed_07:
print("Show News 07")
break
case .Newsfeed_08:
print("Show News 08")
break
}
}
Calling this functions as follows
调用此函数如下
let selCFScreenG = CFScreen.CFScreen_Groups.Create
showCFScreenForEnumType(selCFScreenG)
let selCFScreenNF = CFScreen.CFScreen_Newsfeed.Newsfeed_01
showCFScreenForEnumType(selCFScreenNF)
Taken Reference from link: http://www.codingexplorer.com/enumerations-swift/
从链接中获取参考:http://www.codingexplorer.com/enumerations-swift/
My question is how i can write a function that will accepting enum types generically so that i can merge these below two functions into one
我的问题是我如何编写一个通用接受枚举类型的函数,以便我可以将这两个函数合并为一个
func showCFScreenForEnumType(cFooter: CFScreen.CFScreen_Groups)
func showCFScreenForEnumType(cFooter: CFScreen.CFScreen_Newsfeed)
1 个解决方案
#1
1
You could create a generic type constraint to some protocol, say MyCFScreenEnums
, to which only the two enums in CFScreen
conforms, and within the function that makes use of this (type constrained) generic, perform attempted type conversion to one of the two enums CFScreen.CFScreen_Newsfeed
/CFScreen.CFScreen_Newsfeed
. Since only these two conform to MyCFScreenEnums
, one of these type conversions is guaranteed to be successful due to the type constraint of the generic (say T
) to MyCFScreenEnums
.
您可以为某些协议创建泛型类型约束,例如MyCFScreenEnums,只有CFScreen中的两个枚举符合,并且在使用此(类型约束)泛型的函数内,执行尝试类型转换到两个枚举CFScreen之一.CFScreen_Newsfeed / CFScreen.CFScreen_Newsfeed。由于只有这两个符合MyCFScreenEnums,由于通用(比如T)到MyCFScreenEnums的类型约束,因此保证其中一个类型转换成功。
protocol MyCFScreenEnums { }
extension CFScreen.CFScreen_Newsfeed : MyCFScreenEnums {}
extension CFScreen.CFScreen_Groups : MyCFScreenEnums {}
func showCFScreenForEnumType<T: MyCFScreenEnums>(cFooter: T) {
if let newsfeed = cFooter as? CFScreen.CFScreen_Newsfeed {
switch newsfeed {
case .Newsfeed_01: print("Show News 01")
case .Newsfeed_02: print("Show News 02")
case .Newsfeed_03: print("Show News 03")
case .Newsfeed_04: print("Show News 04")
case .Newsfeed_05: print("Show News 05")
case .Newsfeed_06: print("Show News 06")
case .Newsfeed_07: print("Show News 07")
case .Newsfeed_08: print("Show News 08")
}
}
else if let groups = cFooter as? CFScreen.CFScreen_Groups {
switch groups {
case .Create: print("Show Create Group")
case .GoBack: print("Show Go Back Screen")
}
}
}
/* Example usage */
let foo = CFScreen.CFScreen_Newsfeed.Newsfeed_03
let bar = CFScreen.CFScreen_Groups.Create
showCFScreenForEnumType(foo) // "Show News 03"
showCFScreenForEnumType(bar) // "Show Create Group"
#1
1
You could create a generic type constraint to some protocol, say MyCFScreenEnums
, to which only the two enums in CFScreen
conforms, and within the function that makes use of this (type constrained) generic, perform attempted type conversion to one of the two enums CFScreen.CFScreen_Newsfeed
/CFScreen.CFScreen_Newsfeed
. Since only these two conform to MyCFScreenEnums
, one of these type conversions is guaranteed to be successful due to the type constraint of the generic (say T
) to MyCFScreenEnums
.
您可以为某些协议创建泛型类型约束,例如MyCFScreenEnums,只有CFScreen中的两个枚举符合,并且在使用此(类型约束)泛型的函数内,执行尝试类型转换到两个枚举CFScreen之一.CFScreen_Newsfeed / CFScreen.CFScreen_Newsfeed。由于只有这两个符合MyCFScreenEnums,由于通用(比如T)到MyCFScreenEnums的类型约束,因此保证其中一个类型转换成功。
protocol MyCFScreenEnums { }
extension CFScreen.CFScreen_Newsfeed : MyCFScreenEnums {}
extension CFScreen.CFScreen_Groups : MyCFScreenEnums {}
func showCFScreenForEnumType<T: MyCFScreenEnums>(cFooter: T) {
if let newsfeed = cFooter as? CFScreen.CFScreen_Newsfeed {
switch newsfeed {
case .Newsfeed_01: print("Show News 01")
case .Newsfeed_02: print("Show News 02")
case .Newsfeed_03: print("Show News 03")
case .Newsfeed_04: print("Show News 04")
case .Newsfeed_05: print("Show News 05")
case .Newsfeed_06: print("Show News 06")
case .Newsfeed_07: print("Show News 07")
case .Newsfeed_08: print("Show News 08")
}
}
else if let groups = cFooter as? CFScreen.CFScreen_Groups {
switch groups {
case .Create: print("Show Create Group")
case .GoBack: print("Show Go Back Screen")
}
}
}
/* Example usage */
let foo = CFScreen.CFScreen_Newsfeed.Newsfeed_03
let bar = CFScreen.CFScreen_Groups.Create
showCFScreenForEnumType(foo) // "Show News 03"
showCFScreenForEnumType(bar) // "Show Create Group"