Swift 3:无法将类型'NSMutableDictionary'的值转换为预期的参数类型'[AnyHashable:Any]!" [英] Swift 3: Cannot convert value of type 'NSMutableDictionary' to expected argument type '[AnyHashable : Any]!'

查看:726
本文介绍了Swift 3:无法将类型'NSMutableDictionary'的值转换为预期的参数类型'[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

Int8UInt8Int16UInt16Int32UInt32Int64UInt64以及 IntUIntDoubleFloatCGFloatBool.这些将转换为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, where Element is Objective-C safe

已转换为NSArray.

  • Dictionary,其中KeyValue是Objective-C安全的
  • Dictionary, where Key and Value are Objective-C safe

已转换为NSDictionary.

  • Set,其中Element是Objective-C安全的
  • Set, where Element is Objective-C safe

转换为NSSet

  • NSObject后代类型
  • NSObject descendent types

未转换,按原样使用.

  • 具有对应引用类型的值类型

请参阅列表

  • 值类型,其中NSValue具有用于的初始化器
    • Value types where NSValue has an initializer for

    NSRangeCGPointCGVectorCGSizeCGRectCGAffineTransformUIEdgeInsetsUIOffsetCATransform3DCMTimeCMTimeRangeCMTimeMappingCLLocationCoordinate2DMKCoordinateSpanSCNVector3SCNVector4SCNMatrix4. 这些类型将转换为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.)

    即使在Swift 3.0.1.中,仍然有些值可能会转换为_SwiftValue.

    Still some values may be converted to _SwiftValue even in Swift 3.0.1.

    • 仅Swift类型,例如(仅Swift)枚举,结构,元组...

    (请参见我还没有检查所有包装器的枚举和结构,但是其中一些(例如,从Notification.NameNSString)似乎已经安全地转换了.

    I haven't checked all wrapper enums and structs, but some of them (for example, Notification.Name to NSString) seem to be safely converted.

    • 非可选数字类型和Bool

    IntUIntDoubleFloatCGFloatBool.这些将转换为NSNumber.

    Int, UInt, Double, Float, CGFloat and Bool. These are converted to NSNumber.

    • 非可选String

    已转换为NSString.

    • 非可选Array,其中Element是Objective-C安全的
    • Non-Optional Array, where Element is Objective-C safe

    已转换为NSArray.

    • 非可选Dictionary,其中KeyValue是Objective-C安全的
    • Non-Optional Dictionary, where Key and Value are Objective-C safe

    已转换为NSDictionary.

    • 非可选Set,其中Element是Objective-C安全的
    • Non-Optional Set, where Element is Objective-C safe

    转换为NSSet

    • 非可选的NSObject后代类型
    • Non-Optional NSObject descendent types

    未转换,按原样使用.

    • 具有对应部分引用类型的非可选值类型

    请参阅列表

    See the list here. (The linked article is updated for Swift 3.0.1.)

    这些可能会转换为_SwiftValue,这在Objective-C的世界中是完全没有用的和灾难性的.

    These may be converted to _SwiftValue, which is completely useless and disastrous in Objective-C world.

    • Int8UInt8Int16UInt16Int32UInt32Int64UInt64
    • 任何可选值,包括nil
    • 仅Swift类型,例如(仅Swift)枚举,结构,元组...
    • Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64
    • Any Optional values including nil
    • Swift only types such as (Swift-only)enum, struct, tuple...

    这篇关于Swift 3:无法将类型'NSMutableDictionary'的值转换为预期的参数类型'[AnyHashable:Any]!"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆