Swift 3:无法将类型'NSMutableDictionary'的值转换为预期的参数类型'[AnyHashable:Any]!" [英] Swift 3: Cannot convert value of type 'NSMutableDictionary' to expected argument type '[AnyHashable : Any]!'
问题描述
此代码在Swift 3之前有效.(请问一下Swift 3!)
This code worked before Swift 3. (Curse you Swift 3!)
现在,它在Flurry.logEvent(eventName, withParameters: userData!)
行上显示此错误:
Now it's showing this error against the Flurry.logEvent(eventName, withParameters: userData!)
line:
无法将"NSMutableDictionary"类型的值转换为预期值 参数类型'[[AnyHashable:Any]!'
Cannot convert value of type 'NSMutableDictionary' to expected argument type '[AnyHashable : Any]!'
将userData!
投射到[AnyHashable : Any]
会产生此错误:
Casting userData!
to [AnyHashable : Any]
produces this error:
无法将类型"NSMutableDictionary"的值转换为类型 强制使用[[AnyHashable:Any]"
Cannot convert value of type 'NSMutableDictionary' to type '[AnyHashable : Any]' in coercion
func logEvent(_ eventName: String, userData: NSMutableDictionary?) {
// Use <userData> or create new one?
var userData = userData
if userData == nil {
userData = NSMutableDictionary()
}
// Set base properties
userData!.setObject(gUser.tofus.count, forKey: "Num Tofus" as NSCopying)
userData!.setObject(gUser.getLifetimeTofus(), forKey: "Num Lifetime Tofus" as NSCopying)
// Call Flurry
DispatchQueue.main.async {
Flurry.logEvent(eventName, withParameters: userData! as [AnyHashable:Any])
}
}
Swift 3的正确语法是什么?
What's the right syntax for Swift 3?
推荐答案
如果Flurry.logEvent(_:withParameters:)
占用了[AnyHashable: Any]
,为什么不将其用作本地userData
?
If that Flurry.logEvent(_:withParameters:)
takes [AnyHashable: Any]
, why don't you use it as your local userData
?
func logEvent(_ eventName: String, userData: NSMutableDictionary?) {
// Use <userData> or create new one?
var userData = userData as NSDictionary? as? [AnyHashable: Any] ?? [:]
// Set base properties
userData["Num Tofus"] = gUser.tofus.count
userData["Num Lifetime Tofus"] = gUser.getLifetimeTofus()
// Call Flurry
DispatchQueue.main.async {
Flurry.logEvent(eventName, withParameters: userData)
}
}
更新
Xcode 8.1 GM种子,包括 SE-0139 和
Xcode 8.1 GM seed including SE-0139 and SE-0140 is out, so the list below is updated.
当设置为[AnyHashable: Any]
字典(或在[Any]
数组中设置,或者只是传递给非空的Any
时),这些是 Objective-C安全类型. id
在Objective-C中)传递给了Objective-C世界:
These are the Objective-C safe types, when set to a [AnyHashable: Any]
dictionary (or set in a [Any]
array, or simply passed to Any
which is a non-null id
in Objective-C) in which is passed to Objective-C world:
- 可选值,包括
nil
nil
转换为NSNull
,所有非null的Optional都已解包.
nil
is converted to NSNull
, all non-nil Optionals are unwrapped.
(NSNull
可能不是您想要的.对零校验仍然要小心.)
(NSNull
may not be what you want. Still be careful about nil-checking.)
- 所有数字类型和
Bool
- All numeric types and
Bool
Int8
,UInt8
,Int16
,UInt16
,Int32
,UInt32
,Int64
,UInt64
以及
Int
,UInt
,Double
,Float
,CGFloat
和Bool
.这些将转换为NSNumber
.
Int8
, UInt8
, Int16
, UInt16
, Int32
, UInt32
, Int64
, UInt64
, as well as
Int
, UInt
, Double
, Float
, CGFloat
and Bool
. These are converted to NSNumber
.
-
String
已转换为NSString
.
-
Array
,其中Element
是Objective-C安全的
Array
, whereElement
is Objective-C safe
已转换为NSArray
.
-
Dictionary
,其中Key
和Value
是Objective-C安全的
Dictionary
, whereKey
andValue
are Objective-C safe
已转换为NSDictionary
.
-
Set
,其中Element
是Objective-C安全的
Set
, whereElement
is Objective-C safe
转换为NSSet
-
NSObject
后代类型
NSObject
descendent types
未转换,按原样使用.
- 具有对应引用类型的值类型
请参阅列表
即使在Swift 3.0.1.中,仍然有些值可能会转换为 Still some values may be converted to (请参见我还没有检查所有包装器的枚举和结构,但是其中一些(例如,从 I haven't checked all wrapper enums and structs, but some of them (for example, 已转换为 已转换为 已转换为 转换为 未转换,按原样使用. See the list here. (The linked article is updated for Swift 3.0.1.) 这些可能会转换为 These may be converted to 这篇关于Swift 3:无法将类型'NSMutableDictionary'的值转换为预期的参数类型'[AnyHashable:Any]!"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!NSValue
具有用于的初始化器
NSValue
has an initializer forNSRange
,
CGPoint
,
CGVector
,
CGSize
,
CGRect
,
CGAffineTransform
,
UIEdgeInsets
,
UIOffset
,
CATransform3D
,
CMTime
,
CMTimeRange
,
CMTimeMapping
,
CLLocationCoordinate2D
,
MKCoordinateSpan
,
SCNVector3
,
SCNVector4
,
SCNMatrix4
.
这些类型将转换为NSValue
. (NSRange
在较早的Swifts中已经可以转换为NSValue
,但是没有充分的文档.) NSRange
,
CGPoint
,
CGVector
,
CGSize
,
CGRect
,
CGAffineTransform
,
UIEdgeInsets
,
UIOffset
,
CATransform3D
,
CMTime
,
CMTimeRange
,
CMTimeMapping
,
CLLocationCoordinate2D
,
MKCoordinateSpan
,
SCNVector3
,
SCNVector4
,
SCNMatrix4
.
These types are converted to NSValue
. (NSRange
was already convertible to NSValue
in older Swifts, but not well-documented.)_SwiftValue
._SwiftValue
even in Swift 3.0.1.
Notification.Name
到NSString
)似乎已经安全地转换了.Notification.Name
to NSString
) seem to be safely converted.
Bool
Int
,UInt
,Double
,Float
,CGFloat
和Bool
.这些将转换为NSNumber
.Int
, UInt
, Double
, Float
, CGFloat
and Bool
. These are converted to NSNumber
.
String
NSString
.
Array
,其中Element
是Objective-C安全的
Array
, where Element
is Objective-C safeNSArray
.
Dictionary
,其中Key
和Value
是Objective-C安全的
Dictionary
, where Key
and Value
are Objective-C safeNSDictionary
.
Set
,其中Element
是Objective-C安全的
Set
, where Element
is Objective-C safeNSSet
NSObject
后代类型
NSObject
descendent types
_SwiftValue
,这在Objective-C的世界中是完全没有用的和灾难性的._SwiftValue
, which is completely useless and disastrous in Objective-C world.
Int8
,UInt8
,Int16
,UInt16
,Int32
,UInt32
,Int64
,UInt64
nil
Int8
, UInt8
, Int16
, UInt16
, Int32
, UInt32
, Int64
, UInt64
nil