从字典数组映射到 Swift 中的数字数组
Mapping from array of dictionaries to array of numbers in Swift
这是一个函数,它应该将包含 Dictionary<String, Any>
的订单 NSArray
的键值对数组转换为每个订单 ([NSNumber]
) 的 ID 数组。
但是,我还是遇到了类型转换的问题,错误:
Type 'Any' has no subscript members
如何在Swift中干净地执行映射?
@objc static func ordersLoaded(notification:Notification) -> [NSNumber] {
// Function receives a Notification object from Objective C
let userInfo:Dictionary = notification.userInfo as! Dictionary<String, Any>
// orders is an array of key-value pairs for each order Dictionary<String,Any>
let ordersWithKeyValuePairs:NSArray = userInfo["orders"] as! NSArray // Here a typed array of Dictionaries would be preferred
// it needs to be simplified to an array of IDs for each order (NSNumber)
// orderID is one of the keys
let orderIDs:[NSNumber];
orderIDs = ordersWithKeyValuePairs.flatMap({[=11=]["orderID"] as? NSNumber}) // Line with the error
/*
orderIDs = ordersWithKeyValuePairs.map({
(key,value) in
if key==["orderID"] {
return value
} else {
return nil
}
}) as! [NSNumber]
*/
return orderIDs
}
你可以试试这个
if let ordersWithKeyValuePairs = userInfo["orders"] as? [[String:Any]] {
let result = ordersWithKeyValuePairs.compactMap{[=10=]["orderID"] as? Int }
}
这是有效的方法,将 ordersWithKeyValuePairs
转换为 [Dictionary<String, Any>]
解决了我的问题:
@objc static func ordersLoaded(notification:Notification) -> [NSNumber] {
// Function receives a Notification object from Objective C
let userInfo:Dictionary = notification.userInfo as! Dictionary<String, Any>
// orders is an array of key-value pairs for each order Dictionary<String,Any>
let ordersWithKeyValuePairs:[Dictionary<String, Any>] = userInfo["orders"] as! [Dictionary<String, Any>]
// it needs to be simplified to an array of IDs for each order (NSNumber)
// orderID is one of the keys
let orderIDs:[NSNumber];
orderIDs = ordersWithKeyValuePairs.flatMap({[=10=]["orderID"] as? NSNumber}) // Line with the error
return orderIDs
}
这是一个函数,它应该将包含 Dictionary<String, Any>
的订单 NSArray
的键值对数组转换为每个订单 ([NSNumber]
) 的 ID 数组。
但是,我还是遇到了类型转换的问题,错误:
Type 'Any' has no subscript members
如何在Swift中干净地执行映射?
@objc static func ordersLoaded(notification:Notification) -> [NSNumber] {
// Function receives a Notification object from Objective C
let userInfo:Dictionary = notification.userInfo as! Dictionary<String, Any>
// orders is an array of key-value pairs for each order Dictionary<String,Any>
let ordersWithKeyValuePairs:NSArray = userInfo["orders"] as! NSArray // Here a typed array of Dictionaries would be preferred
// it needs to be simplified to an array of IDs for each order (NSNumber)
// orderID is one of the keys
let orderIDs:[NSNumber];
orderIDs = ordersWithKeyValuePairs.flatMap({[=11=]["orderID"] as? NSNumber}) // Line with the error
/*
orderIDs = ordersWithKeyValuePairs.map({
(key,value) in
if key==["orderID"] {
return value
} else {
return nil
}
}) as! [NSNumber]
*/
return orderIDs
}
你可以试试这个
if let ordersWithKeyValuePairs = userInfo["orders"] as? [[String:Any]] {
let result = ordersWithKeyValuePairs.compactMap{[=10=]["orderID"] as? Int }
}
这是有效的方法,将 ordersWithKeyValuePairs
转换为 [Dictionary<String, Any>]
解决了我的问题:
@objc static func ordersLoaded(notification:Notification) -> [NSNumber] {
// Function receives a Notification object from Objective C
let userInfo:Dictionary = notification.userInfo as! Dictionary<String, Any>
// orders is an array of key-value pairs for each order Dictionary<String,Any>
let ordersWithKeyValuePairs:[Dictionary<String, Any>] = userInfo["orders"] as! [Dictionary<String, Any>]
// it needs to be simplified to an array of IDs for each order (NSNumber)
// orderID is one of the keys
let orderIDs:[NSNumber];
orderIDs = ordersWithKeyValuePairs.flatMap({[=10=]["orderID"] as? NSNumber}) // Line with the error
return orderIDs
}