简体   繁体   English

如何将Objective-c中的数据类型与Swift协议匹配?

[英]How to match data types in Objective-c to Swift protocol?

Trying to port Objective-c code to Swift and I'm pretty new to Swift. 尝试将Objective-c代码移植到Swift时,我对Swift还是一个新手。 I added bridging header and included all the necessary headers to it. 我添加了桥接标头,并包括了所有必需的标头。 In my Swift class I need to use the following protocol below: 在我的Swift类中,我需要使用以下协议:

@protocol MyProtocolDelegate

- (void)doSomeStuff:(NSDictionary *)stuff;

@end

In my Swift class I have the following code: 在我的Swift类中,我有以下代码:

func doSomeStuff(stuff:Dictionary<String,String>)
{
...
}

I get an error here: 我在这里得到一个错误:

class ViewController: UIViewController,MyProtocolDelegate  <-- Type ViewController doesn't conform to protocol MyProtocolDelegate

Any ideas why I'm getting this error? 有任何想法为什么我会收到此错误吗?

Any kind of help is highly appreciated! 任何帮助都将受到高度赞赏!

As you've discovered, a plain NSDictionary * is bridged to [NSObject: AnyObject]! 如您[NSObject: AnyObject]! ,普通的NSDictionary *被桥接到[NSObject: AnyObject]! .

To get Dictionary<String,String> , you would have to declare it in Objective-C as 要获取Dictionary<String,String> ,您必须在Objective-C中将其声明为

- (void)doSomeStuff:(nonnull NSDictionary<NSString *, NSString *> *)stuff;
func doSomeStuff(stuff: NSDictionary)

如果不想,则无需指定类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM