Swift 4.1 - 无法将类型 [Character] 的值转换为 [String]
Swift 4.1 - Cannot convert value of type [Character] to [String]
我的代码之前支持 Swift 3.3,现在我使用 Xcode 9.3 将其升级到 Swift 4.1。它在尝试构建我的项目时向我显示以下错误。
这里是JSON解析的代码
// MARK: Store JSON initializer
convenience init?(withJSON json: JSON) {
//json mapping
//This is of type [Character] after mapping
let services = json["services"].arrayValue.flatMap({ [=11=] }).flatMap({ [=11=].1.stringValue }) //Convert response to 1D array and convert it to array if String
}
这是我要调用的初始化方法
//Custom init method
init(id: Int, storeNumber: String, title: String, location: Location, type: String, zip: String, parent: Int, city: String, services: [String], state: String, storePhone: [String], pharmacyPhone: [String], pharmacyFax: [String], workingHours: WorkingHoursString) {
//field with error
//here self.services is of type [String]
self.services = services
}
我正在使用
pod 'SwiftyJSON', '3.1.4' 用于Json解析。
Error - Cannot convert value of type '[character]' to expected argument
type '[String]'
/* JSON for services is as given
"services":[
[
"Fresh Food"
]
]
*/
print("Services are \(services)")
Services are ["F", "r", "e", "s", "h", " ", "F", "o", "o", "d"]
解决此问题的最简单解决方案是什么?
可以在以下代码中观察到相同的行为:
let services: [[String: Any]?] = [
["service1": "service1-name"],
["service2": "service2-name"]
]
let result = services
.flatMap({ [=10=] })
.flatMap({ [=10=].1 as! String })
print(result)
我认为这是由于 String
和 Dictionary
在 Swift 4 中的多次变化造成的(例如 String
变成了 Collection
个字符).在上面的代码中,第一个 flatMap
将字典合并(展平)到一个字典中,第二个 flatMap
将每个值作为 String
并将它们展平为二维 Collection
Character
.
我想你想要这样的东西:
let result = services
.compactMap { [=11=] } // remove nil dictionaries
.flatMap { // take all dictionary values as strings and flatten them to an array
[=11=].values.map { [=11=].stringValue }
}
print(result)
这一行给出了一个字符串数组,预期结果
let services = json["services"]
.arrayValue
.flatMap { [=12=].arrayValue }
.map { [=12=].stringValue }
我的代码之前支持 Swift 3.3,现在我使用 Xcode 9.3 将其升级到 Swift 4.1。它在尝试构建我的项目时向我显示以下错误。
这里是JSON解析的代码
// MARK: Store JSON initializer
convenience init?(withJSON json: JSON) {
//json mapping
//This is of type [Character] after mapping
let services = json["services"].arrayValue.flatMap({ [=11=] }).flatMap({ [=11=].1.stringValue }) //Convert response to 1D array and convert it to array if String
}
这是我要调用的初始化方法
//Custom init method
init(id: Int, storeNumber: String, title: String, location: Location, type: String, zip: String, parent: Int, city: String, services: [String], state: String, storePhone: [String], pharmacyPhone: [String], pharmacyFax: [String], workingHours: WorkingHoursString) {
//field with error
//here self.services is of type [String]
self.services = services
}
我正在使用 pod 'SwiftyJSON', '3.1.4' 用于Json解析。
Error - Cannot convert value of type '[character]' to expected argument type '[String]'
/* JSON for services is as given
"services":[
[
"Fresh Food"
]
]
*/
print("Services are \(services)")
Services are ["F", "r", "e", "s", "h", " ", "F", "o", "o", "d"]
解决此问题的最简单解决方案是什么?
可以在以下代码中观察到相同的行为:
let services: [[String: Any]?] = [
["service1": "service1-name"],
["service2": "service2-name"]
]
let result = services
.flatMap({ [=10=] })
.flatMap({ [=10=].1 as! String })
print(result)
我认为这是由于 String
和 Dictionary
在 Swift 4 中的多次变化造成的(例如 String
变成了 Collection
个字符).在上面的代码中,第一个 flatMap
将字典合并(展平)到一个字典中,第二个 flatMap
将每个值作为 String
并将它们展平为二维 Collection
Character
.
我想你想要这样的东西:
let result = services
.compactMap { [=11=] } // remove nil dictionaries
.flatMap { // take all dictionary values as strings and flatten them to an array
[=11=].values.map { [=11=].stringValue }
}
print(result)
这一行给出了一个字符串数组,预期结果
let services = json["services"]
.arrayValue
.flatMap { [=12=].arrayValue }
.map { [=12=].stringValue }