如何在 swift 中指定 NSDictionary 参数的键和值数据类型?
我想在 swift 中定义一个也可以在 Objective-C 中使用的函数。我有一个枚举:
@objc public enum BookType: Int {
case novel
case magazine
func name() -> String {
switch self {
case .novel:
return "novel"
case .magazine:
return "magazine"
}
}
}
函数是:
@objc public func uploadBookImages(images: [BookType: UIImage]) {
// code here
}
这个函数会给我带来一个编译错误说:
方法不能被标记@objc,因为参数的类型不能 在 Objective-C 中表示
这可能是因为 Objective-C 不接受 swift 字典,因为它是一个结构体。我正在考虑使用 NSDictionary 而不是 swift 字典。但是,如何使用 NSDictionary 将参数数据类型指定为 [BookType: UIImage]?我想传递 NSDictionary
I would like to define a function in swift that can be used in objective-c as well. I have a enum:
@objc public enum BookType: Int {
case novel
case magazine
func name() -> String {
switch self {
case .novel:
return "novel"
case .magazine:
return "magazine"
}
}
}
And the function is:
@objc public func uploadBookImages(images: [BookType: UIImage]) {
// code here
}
This function will bring me an compile error said that:
Method cannot be marked @objc because the type of the parameter cannot
be represented in Objective-C
It might because objective-c doesn't accept swift dictionary since it is a structure. I am thinking to use NSDictionary instead of a swift dictionary. However, how can I specify the argument data type to [BookType: UIImage] using a NSDictionary? I want to pass a NSDictionary<BookType: UIImage> to the function when I call it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用 NSDictionary 而不是 swift Dictionary
用法:
You need to use NSDictionary instead of swift Dictionary
Usage: