Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在Swift中将字典对象传递给目标C协议_Ios_Objective C_Swift_Dictionary_Protocols - Fatal编程技术网

Ios 在Swift中将字典对象传递给目标C协议

Ios 在Swift中将字典对象传递给目标C协议,ios,objective-c,swift,dictionary,protocols,Ios,Objective C,Swift,Dictionary,Protocols,我正在尝试使用swift将dictionary对象传递给Objective C协议 协议代码片段如下所示: @protocol MessageDelegate - (void)handleNewMessageArrived:(NSDictionary *)messageContent; @end 这是实现协议的swift类: class ViewController: UIViewController, MessageDelegate { ... func handleNewMess

我正在尝试使用swift将dictionary对象传递给Objective C协议

协议代码片段如下所示:

@protocol MessageDelegate

- (void)handleNewMessageArrived:(NSDictionary *)messageContent;

@end
这是实现协议的swift类:

class ViewController: UIViewController, MessageDelegate
{
 ...

 func handleNewMessageArrived(messageContent : NSDictionary!)
 {
  ...    
 }
}
但构建失败,我得到的错误是:

类型'ViewController'不符合协议'MessageDelegate

我查看了,但它处理的是特定的对象类型

我声明\实现委托方法的方式是否有错误?或者我假设这些参数是用swift映射的


我是Swift新手,因此非常感谢您的帮助。

尝试在您的Swift类中实现此方法,如下所示:

func handleNewMessageArrived(messageContent: [NSObject : AnyObject]!) {
    // Handle the message
}

尝试在Swift类中实现该方法,如下所示:

func handleNewMessageArrived(messageContent: [NSObject : AnyObject]!) {
    // Handle the message
}

对于Swift 3,这是您需要的

func handleNewMessageArrived(messageContent: [AnyHashable : Any]!) {
    // Handle the message
}

对于Swift 3,这是您需要的

func handleNewMessageArrived(messageContent: [AnyHashable : Any]!) {
    // Handle the message
}

@哦,我没有花太多时间和斯威夫特在一起。您能解释一下为什么必须在Swift中将messageContent参数设置为匿名指针吗?它不是Objective-C中的id类型(等价物)。@duncac
messageContent
参数是一个隐式展开的可选参数,包含带有
NSObject
键和
AnyObject
值的Swift字典。这与
NSDictionary
密切相关,是Swift自动桥接的结果。因此,默认情况下,像dictionary这样的容器对象是特定类型的容器(例如字符串字典)。NSDictionary不是具体类型,而是Swift中的有效参数吗?所有Swift容器都是类型安全的(一件好事)
NSDictionary
在Swift中也是有效的类型,但是Swift Objective-C桥会自动转换类型。@sbooth我在Swift上没有花太多时间。您能解释一下为什么必须在Swift中将messageContent参数设置为匿名指针吗?它不是Objective-C中的id类型(等价物)。@duncac
messageContent
参数是一个隐式展开的可选参数,包含带有
NSObject
键和
AnyObject
值的Swift字典。这与
NSDictionary
密切相关,是Swift自动桥接的结果。因此,默认情况下,像dictionary这样的容器对象是特定类型的容器(例如字符串字典)。NSDictionary不是具体类型,而是Swift中的有效参数吗?所有Swift容器都是类型安全的(一件好事)
NSDictionary
也是Swift中的有效类型,但Swift Objective-C桥接器会自动转换这些类型。