如何在 swift 中转换混合大小的整数数组
How to convert an array of mixed sized integers in swift
Swift5,iOS14
我在 Swift 中玩过泛型,试图理解它们并编造了一个似乎无法解决的问题。假设我有一个混合大小的整数数组。
var dindex = [Int8(1),Int16(2),Int32(3),Int64(4)] as [Any]
我想将它们转换为双精度数组以进行处理。现在 map 不起作用,我也不能它似乎只是单步遍历数组,此时我对泛型没有运气......
// does not work
func convertMap(qindexIn qindexOut:[Int]) -> [Double] {
return(qindexOut.map({ Double([=12=]) }))
}
// does not work
func convertMap22(qindexIn qindexOut:[Any]) -> [Double] {
let foo:[Double] = []
for i in qindexOut {
foo.append(Double(i))
}
return(foo)
}
// does not work
func convertMap2<T: BinaryInteger>(qindexIn qindexOut:[T]) -> [Double] {
return(qindexOut.map({ Double([=12=]) }))
}
// this doesn't work either
func convertMap22(qindexIn qindexOut:[Any]) -> [Int] {
return(qindexOut.compactMap { [=12=] as? Int })
}
事实上,大部分代码甚至都无法编译。这里有什么诀窍?一定可以吗?
这会奏效。我将 Any 转换为 Int 类型
func convertMap22(qindexIn qindexOut:[Any]) -> [Double] {
var foo:[Double] = []
for i in qindexOut {
if let intValue = i as? Int{
let doubleValue = Double(intValue)
foo.append(doubleValue)
}else if let intValue = i as? Int8{
let doubleValue = Double(intValue)
foo.append(doubleValue)
}else if let intValue = i as? Int16{
let doubleValue = Double(intValue)
foo.append(doubleValue)
}else if let intValue = i as? Int32{
let doubleValue = Double(intValue)
foo.append(doubleValue)
}else if let intValue = i as? Int64{
let doubleValue = Double(intValue)
foo.append(doubleValue)
}
}
return(foo)
}
Any
in Swift 真的很难相处。
请试试这个:
func convertMap(qindexIn qindexOut:[Any]) -> [Double] {
return(qindexOut.map{ ([=10=] as? NSNumber ?? NSNumber(0.0)).doubleValue })
}
希望有更好的方法。
看着 OOPer 的答案给了我一个想法,这也有效,虽然不是更 Swifty 或许更少 Objective C'ish。
func convertMap26(qindexIn qindexOut:[Any]) -> [Double] {
return(qindexOut.map{ Double("\([=10=])") ?? 0.0 })
}
Swift5,iOS14
我在 Swift 中玩过泛型,试图理解它们并编造了一个似乎无法解决的问题。假设我有一个混合大小的整数数组。
var dindex = [Int8(1),Int16(2),Int32(3),Int64(4)] as [Any]
我想将它们转换为双精度数组以进行处理。现在 map 不起作用,我也不能它似乎只是单步遍历数组,此时我对泛型没有运气......
// does not work
func convertMap(qindexIn qindexOut:[Int]) -> [Double] {
return(qindexOut.map({ Double([=12=]) }))
}
// does not work
func convertMap22(qindexIn qindexOut:[Any]) -> [Double] {
let foo:[Double] = []
for i in qindexOut {
foo.append(Double(i))
}
return(foo)
}
// does not work
func convertMap2<T: BinaryInteger>(qindexIn qindexOut:[T]) -> [Double] {
return(qindexOut.map({ Double([=12=]) }))
}
// this doesn't work either
func convertMap22(qindexIn qindexOut:[Any]) -> [Int] {
return(qindexOut.compactMap { [=12=] as? Int })
}
事实上,大部分代码甚至都无法编译。这里有什么诀窍?一定可以吗?
这会奏效。我将 Any 转换为 Int 类型
func convertMap22(qindexIn qindexOut:[Any]) -> [Double] {
var foo:[Double] = []
for i in qindexOut {
if let intValue = i as? Int{
let doubleValue = Double(intValue)
foo.append(doubleValue)
}else if let intValue = i as? Int8{
let doubleValue = Double(intValue)
foo.append(doubleValue)
}else if let intValue = i as? Int16{
let doubleValue = Double(intValue)
foo.append(doubleValue)
}else if let intValue = i as? Int32{
let doubleValue = Double(intValue)
foo.append(doubleValue)
}else if let intValue = i as? Int64{
let doubleValue = Double(intValue)
foo.append(doubleValue)
}
}
return(foo)
}
Any
in Swift 真的很难相处。
请试试这个:
func convertMap(qindexIn qindexOut:[Any]) -> [Double] {
return(qindexOut.map{ ([=10=] as? NSNumber ?? NSNumber(0.0)).doubleValue })
}
希望有更好的方法。
看着 OOPer 的答案给了我一个想法,这也有效,虽然不是更 Swifty 或许更少 Objective C'ish。
func convertMap26(qindexIn qindexOut:[Any]) -> [Double] {
return(qindexOut.map{ Double("\([=10=])") ?? 0.0 })
}