If that Flurry.logEvent(_:withParameters:)
takes [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)
}
}
UPDATE
Xcode 8.1 GM 种子包括SE-0139 https://github.com/apple/swift-evolution/blob/master/proposals/0139-bridge-nsnumber-and-nsvalue.md and SE-0140 https://github.com/apple/swift-evolution/blob/master/proposals/0140-bridge-optional-to-nsnull.md已发布,因此下面的列表已更新。
这些是Objective-C 安全类型,当设置为[AnyHashable: Any]
字典(或设置在[Any]
数组,或者简单地传递给Any
这是一个非空值id
在 Objective-C 中),其中传递到 Objective-C 世界:
斯威夫特 3.0.1/Xcode 8.1
nil
被转换为NSNull
,所有非零选项都被展开。
(NSNull
可能不是你想要的。仍然要小心 nil 检查。)
Int8
, UInt8
, Int16
, UInt16
, Int32
, UInt32
, Int64
, UInt64
, 也Int
, UInt
, Double
, Float
, CGFloat
and Bool
。这些被转换为NSNumber
.
转换成NSString
.
-
Array
, where Element
Objective-C 是安全的
转换成NSArray
.
-
Dictionary
, where Key
and Value
Objective-C 是安全的
转换成NSDictionary
.
-
Set
, where Element
Objective-C 是安全的
转换成NSSet
未转换,按原样使用。
查看列表here https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/BuildingCocoaApps/WorkingWithCocoaDataTypes.html#//apple_ref/doc/uid/TP40014216-CH6-ID211.
NSRange
,
CGPoint
,
CGVector
,
CGSize
,
CGRect
,
CGAffineTransform
,
UIEdgeInsets
,
UIOffset
,
CATransform3D
,
CMTime
,
CMTimeRange
,
CMTimeMapping
,
CLLocationCoordinate2D
,
MKCoordinateSpan
,
SCNVector3
,
SCNVector4
,
SCNMatrix4
。
这些类型被转换为NSValue
. (NSRange
已经可以转换为NSValue
在较旧的 Swift 中,但没有详细记录。)
坏事(示例)
仍有一些值可以转换为_SwiftValue
即使在 Swift 3.0.1 中也是如此。
- 仅限 Swift 的类型,例如(仅限 Swift)enum、struct、tuple...
(See 这个清单 https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithObjective-CAPIs.html#//apple_ref/doc/uid/TP40014216-CH4-ID53.)
我没有检查所有包装器枚举和结构,但检查了其中一些(例如,Notification.Name
to NSString
)似乎已安全转换。
斯威夫特 3.0.0/Xcode 8.0
Int
, UInt
, Double
, Float
, CGFloat
and Bool
。这些被转换为NSNumber
.
转换成NSString
.
- 非可选
Array
, where Element
Objective-C 是安全的
转换成NSArray
.
- 非可选
Dictionary
, where Key
and Value
Objective-C 是安全的
转换成NSDictionary
.
- 非可选
Set
, where Element
Objective-C 是安全的
转换成NSSet
未转换,按原样使用。
查看列表here https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/BuildingCocoaApps/WorkingWithCocoaDataTypes.html#//apple_ref/doc/uid/TP40014216-CH6-ID211。 (链接的文章已针对 Swift 3.0.1 进行更新。)
坏事(示例)
这些可以转换为_SwiftValue
,这在 Objective-C 世界中是完全无用且灾难性的。
-
Int8
, UInt8
, Int16
, UInt16
, Int32
, UInt32
, Int64
, UInt64
- 任何可选值包括
nil
- 仅限 Swift 的类型,例如(仅限 Swift)enum、struct、tuple...