如果 Flurry.logEvent(_:withParameters:) 采用 [AnyHashable: Any],为什么不将其用作本地 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)
}
}
更新
包含SE-0139 和SE-0140 的Xcode 8.1 GM 种子已发布,因此更新了以下列表。
这些是 Objective-C 安全 类型,当设置为 [AnyHashable: Any] 字典(或设置在 [Any] 数组中,或简单地传递给非空的 Any id in Objective-C) 在其中传递给 Objective-C 世界:
斯威夫特 3.0.1/Xcode 8.1
nil 转换为 NSNull,所有非 nil 选项都被解包。
(NSNull 可能不是你想要的。还是要小心 nil-checking。)
Int8、UInt8、Int16、UInt16、Int32、UInt32、Int64、UInt64,以及
Int、UInt、Double、Float、CGFloat 和 Bool。这些被转换为NSNumber。
转换为NSString。
-
Array,其中 Element 是 Objective-C 安全的
转换为NSArray。
-
Dictionary,其中 Key 和 Value 是 Objective-C 安全的
转换为NSDictionary。
-
Set,其中 Element 是 Objective-C 安全的
转换为NSSet
未转换,按原样使用。
查看列表here。
NSRange,
CGPoint,
CGVector,
CGSize,
CGRect,
CGAffineTransform,
UIEdgeInsets,
UIOffset,
CATransform3D,
CMTime,
CMTimeRange,
CMTimeMapping,
CLLocationCoordinate2D,
MKCoordinateSpan,
SCNVector3,
SCNVector4,
SCNMatrix4。
这些类型转换为NSValue。 (NSRange 在旧 Swift 中已经可以转换为 NSValue,但没有详细记录。)
坏事(例子)
即使在 Swift 3.0.1 中,仍有一些值可能会转换为 _SwiftValue。
- 仅限 Swift 的类型,例如(仅限 Swift)枚举、结构、元组...
(见this list。)
我没有检查所有包装枚举和结构,但其中一些(例如,Notification.Name 到 NSString)似乎已安全转换。
Swift 3.0.0/Xcode 8.0
Int、UInt、Double、Float、CGFloat 和 Bool。这些被转换为NSNumber。
转换为NSString。
- 非可选
Array,其中 Element 是 Objective-C 安全的
转换为NSArray。
- 非可选的
Dictionary,其中Key 和Value 是Objective-C 安全的
转换为NSDictionary。
- 非可选
Set,其中Element 是Objective-C 安全的
转换为NSSet
未转换,按原样使用。
查看列表here。 (链接的文章针对 Swift 3.0.1 进行了更新。)
坏事(例子)
这些可能会被转换成_SwiftValue,这在Objective-C世界中是完全没用的和灾难性的。
-
Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64
- 任何可选值,包括
nil
- 仅限 Swift 的类型,例如(仅限 Swift)枚举、结构、元组...