在字典键值对中,如何将值(其中value可以是不同类型的类型)解包为String

问题描述 投票:0回答:1

Eureka表单值返回可选值,所以我想遍历每个值并将其解包。但是,这些值由Double,Int,String组成。如何将它们解包为String?

    let wrapped = self.formValues()

    var unwrapped = [String:Any]()

    for (k,v) in wrapped {
        unwrapped[k] = v as! String ?? ""
    }
    return unwrapped

我收到此错误:

    Could not cast value of type 'Swift.Double' (0x7fff879c1a10) to 'Swift.String' (0x7fff879c41c8).
ios swift dictionary eureka-forms
1个回答
0
投票

替换

v as! String ?? ""

with

String(v)
© www.soinside.com 2019 - 2024. All rights reserved.