答案 0 :(得分:6)
我担心它不可能,Apple有一个 Swift类型兼容性列表,它明确排除了没有Int
原始值类型的Swift中定义的枚举。 / p>
答案 1 :(得分:3)
这就是我所做的:
在Swift类中创建了枚举
enum Origin {
case Search(searchTerm: String, searchResultsPageNum: Int)
case Discovery(pageNum: Int)
}
然后在我的班级中,创建enum属性和函数(对Objective C可见)以设置和获取enum属性的值。
@objc class GameSession: NSObject
{
...
var gameOrigin: Origin?
...
let originStr = "origin"
let notSpecified = "Not Specified"
@objc func getOrigin() -> NSDictionary
{
guard let origin = gameOrigin else {
return [originStr: notSpecified]
}
switch origin {
case .Search(let searchTerm, let searchResultsPageNum):
return ["searchTerm": searchTerm, "searchResultsPageNum": "\(searchResultsPageNum)"]
case .Discovery(let pageNum)
return ["pageNum": pageNum]
default:
return [originStr: notSpecified]
}
}
@objc func setSearchOriginWith(searchTerm: String, searchResultsPageNum: Int, filtered:Bool)
{
self.gameOrigin = Origin.Search(searchTerm: searchTerm, searchResultsPageNum: searchResultsPageNum, filtered: filtered)
}
@objc func setDiscoveryOriginWith(pageNum: Int)
{
self.gameOrigin = Origin.Discovery(pageNum: pageNum)
}
}