SwiftUI 枚举字符串、可编码、可哈希错误
SwiftUI enum String, Codable, Hashable Error
import SwiftUI
import Foundation
import Combine
import CoreLocation
struct structuralCodes: Hashable, Codable, Identifiable {
var id = UUID()
var index: Int
var title: String
var icon: String
var isFeatured: Bool
var category: Category
enum Category: String, CaseIterable, Codable, Hashable {
case asce = "ASCE"
case aisc = "AISC"
}
var imageName: String
var image: Image {
Image(imageName)
}
}
var structuralcodes = [
structuralCodes(index: 1, title: "ASCE", icon: "wind", isFeatured: true, category: "ASCE", imageName: "ASCE"),
structuralCodes(index: 2, title: "AISC", icon: "wind", isFeatured: false, category: "AISC", imageName: "AISC"),
]
import SwiftUI
import Foundation
import Combine
final class ModelData: ObservableObject {
@Published var choosecodes: [structuralCodes] = structuralcodes
var features: [structuralCodes] {
structuralcodes.filter { [=10=].isFeatured }
}
var categories: [String: [structuralCodes]] {
Dictionary(
grouping: choosecodes,
by: { [=10=].category.rawValue }
)
}
} //: ModelData
我在零件的 var 结构代码中遇到错误
(category: "ASCE") //第一个引号下的错误
(类别:“AISC”)//第一个引号下的错误
错误为:
Cannot convert value of type 'String' to expected argument type
'structuralCodes.Category
我想当我有 String 的枚举时,它会保存在 var 类别下的案例,因为我输入了一个 String。大写和小写都试过了,怎么都报错
你可以试试这个:
var structuralcodes = [
structuralCodes(index: 1, title: "ASCE", icon: "wind", isFeatured: true, category: structuralCodes.Category(rawValue: "ASCE")!, imageName: "ASCE"),
structuralCodes(index: 2, title: "AISC", icon: "wind", isFeatured: false, category: structuralCodes.Category(rawValue: "AISC")!, imageName: "AISC"),
]
所有的Category
都是String
,但不是所有的String
都是Category
,这就是为什么你需要在这里跳过箍。强制解包在这里是安全的,因为你显然分配了一个有效的 rawValue
import SwiftUI
import Foundation
import Combine
import CoreLocation
struct structuralCodes: Hashable, Codable, Identifiable {
var id = UUID()
var index: Int
var title: String
var icon: String
var isFeatured: Bool
var category: Category
enum Category: String, CaseIterable, Codable, Hashable {
case asce = "ASCE"
case aisc = "AISC"
}
var imageName: String
var image: Image {
Image(imageName)
}
}
var structuralcodes = [
structuralCodes(index: 1, title: "ASCE", icon: "wind", isFeatured: true, category: "ASCE", imageName: "ASCE"),
structuralCodes(index: 2, title: "AISC", icon: "wind", isFeatured: false, category: "AISC", imageName: "AISC"),
]
import SwiftUI
import Foundation
import Combine
final class ModelData: ObservableObject {
@Published var choosecodes: [structuralCodes] = structuralcodes
var features: [structuralCodes] {
structuralcodes.filter { [=10=].isFeatured }
}
var categories: [String: [structuralCodes]] {
Dictionary(
grouping: choosecodes,
by: { [=10=].category.rawValue }
)
}
} //: ModelData
我在零件的 var 结构代码中遇到错误
(category: "ASCE") //第一个引号下的错误 (类别:“AISC”)//第一个引号下的错误
错误为:
Cannot convert value of type 'String' to expected argument type 'structuralCodes.Category
我想当我有 String 的枚举时,它会保存在 var 类别下的案例,因为我输入了一个 String。大写和小写都试过了,怎么都报错
你可以试试这个:
var structuralcodes = [
structuralCodes(index: 1, title: "ASCE", icon: "wind", isFeatured: true, category: structuralCodes.Category(rawValue: "ASCE")!, imageName: "ASCE"),
structuralCodes(index: 2, title: "AISC", icon: "wind", isFeatured: false, category: structuralCodes.Category(rawValue: "AISC")!, imageName: "AISC"),
]
所有的Category
都是String
,但不是所有的String
都是Category
,这就是为什么你需要在这里跳过箍。强制解包在这里是安全的,因为你显然分配了一个有效的 rawValue