[英]Type MCSessionState does not conform to protocol 'AnyObject'
I'm getting this error with the following code: 我通过以下代码收到此错误:
var dict: Dictionary<String, AnyObject> = [
"peerID": peerID,
"state": state
]
I'm using MultipeerConnectivity
: peerID is of type MCPeerID (eg, MCPeerID(displayName: "morpheus")
), and state is MCSessionState (an enum, eg, MCSessionState.Connected
). 我正在使用
MultipeerConnectivity
:peerID的类型为MCPeerID(例如, MCPeerID(displayName: "morpheus")
),状态为MCSessionState(一个枚举,例如, MCSessionState.Connected
)。 Apparently, I cannot convert an enum to AnyObject? 显然,我不能将枚举转换为AnyObject吗? How can I solve this?
我该如何解决?
Best. 最好。
Edit: I tried using Dictionary<String, Any>, but now I get an exception in the next call. 编辑:我尝试使用Dictionary <String,Any>,但是现在在下一个调用中出现异常。 Here's the code:
这是代码:
func session(session: MCSession!, peer peerID: MCPeerID!, didChangeState state: MCSessionState) {
var dict: Dictionary<String, Any> = [
"peerID": peerID,
"state": state
]
NSNotificationCenter.defaultCenter().postNotificationName(
"MCDidChangeStateNotification",
object: nil,
userInfo: dict
)
}
Xcode indicates the "userInfo: dict" line with the exception: Xcode指示“ userInfo:dict”行,但例外:
Thread 10: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
Maybe this won't tell you much, so what could I post that would help? 也许这不会告诉您太多,那么我能发布些什么会有所帮助?
Edit: 编辑:
By inspecting the object at runtime (after the crash), this is what I get: 通过在运行时(崩溃后)检查对象,我得到了:
state MultipeerConnectivity.MCSessionState Connecting Connecting
dict Swift.Dictionary<Swift.String, protocol<>>
[0] Swift._DictionaryElement<Swift.String, protocol<>>
key Swift.String "state"
core Swift._StringCore
value protocol<>
payload_data_0 Builtin.RawPointer 0x0
payload_data_1 Builtin.RawPointer 0x0
payload_data_2 Builtin.RawPointer 0x0
instance_type Builtin.RawPointer 0x0
There seem to be some null pointers there, but the "state" variable seems fine... 那里似乎有一些空指针,但是“ state”变量似乎很好...
Note: I commented out the peerID assignment. 注意:我注释了peerID分配。
AnyObject
仅适用于类类型,请使用Any
也支持枚举(参见docs )。
Apparently, I cannot convert an enum to AnyObject?
显然,我不能将枚举转换为AnyObject吗? How can I solve this?
我该如何解决?
That's correct, AnyObject cannot contain enums. 没错,AnyObject不能包含枚举。
I tried using
Dictionary<String, Any>
, but now I get an exception in the next call.我尝试使用
Dictionary<String, Any>
,但是现在在下一个调用中出现异常。
NSNotificationCentre doesn't take a swift Dictionary
, it takes an NSDictionary
. NSNotificationCentre不需要快速的
Dictionary
,它需要一个NSDictionary
。
Wherever possible, Dictionary
and NSDictionary
are inter-operable however NSDictionary
can only have objects as the key and value. Dictionary
和NSDictionary
在任何可能的地方都可以互操作,但是NSDictionary
只能将对象作为键和值。 You cannot use Any
in a dictionary that needs to be treated as if it were an NSDictionary
by an obj-c API such as NSNotificationCentre
. 您不能在字典中使用
Any
需要被obj-c API(例如NSNotificationCentre
视为NSDictionary
。
So, long story short, a notification userInfo dictionary has to be Dictionary<AnyObject, AnyObject>
(or <String, String>
or something that is an object). 因此,长话短说,通知用户信息字典必须是
Dictionary<AnyObject, AnyObject>
(或<String, String>
或作为对象的东西)。 You're going to have to use something other than an Enum. 您将不得不使用除Enum之外的其他东西。
I think you need to use type Any
for the dictionary values (because AnyObject can only be used for an instance of a class): 我认为您需要对字典值使用
Any
类型(因为AnyObject只能用于类的实例):
This worked in a playground for me: 这为我在操场上工作:
var dict: Dictionary<String, Any> = [
"peerID": peerID,
"state": state
];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.