Objective-C中的Swift枚举关联值

时间:2022-09-07 09:37:42
  1. Is there a way to use new Swift3 enums with associated value in Objective-C?
  2. 有没有办法在Objective-C中使用具有相关值的新Swift3枚举?

  3. Is there a way to declare/bridge Swift3 enums with associated value in Objective-C, if I develop a library and want to give Swift3 users convenient API?
  4. 有没有办法在Objective-C中声明/桥接具有相关值的Swift3枚举,如果我开发一个库并想给Swift3用户提供方便的API?

2 个解决方案

#1


5  

I'm afraid it's not possible, Apple has a list of Swift Type Compatibility which explicitly excludes enumerations defined in Swift without Int raw value type.

我担心这是不可能的,Apple有一个Swift类型兼容性列表,它明确排除了没有Int原始值类型的Swift中定义的枚举。

Reference

#2


1  

This is what I did:

这就是我做的:

In Swift class created the enum

在Swift类中创建了枚举

enum Origin {
    case Search(searchTerm: String, searchResultsPageNum: Int)
    case Discovery(pageNum: Int)
}

Then in my Class, created enum property and functions (that are visible to Objective C) to set and get values of the enum property.

然后在我的类中,创建枚举属性和函数(对于Objective C可见)来设置和获取枚举属性的值。

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

}

#1


5  

I'm afraid it's not possible, Apple has a list of Swift Type Compatibility which explicitly excludes enumerations defined in Swift without Int raw value type.

我担心这是不可能的,Apple有一个Swift类型兼容性列表,它明确排除了没有Int原始值类型的Swift中定义的枚举。

Reference

#2


1  

This is what I did:

这就是我做的:

In Swift class created the enum

在Swift类中创建了枚举

enum Origin {
    case Search(searchTerm: String, searchResultsPageNum: Int)
    case Discovery(pageNum: Int)
}

Then in my Class, created enum property and functions (that are visible to Objective C) to set and get values of the enum property.

然后在我的类中,创建枚举属性和函数(对于Objective C可见)来设置和获取枚举属性的值。

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

}