Swift 返回基于字符串的颜色
我试图根据我在结构中声明的字符串枚举选择形状的颜色。我有一个字符串“red”,我想将其转换为“Color.red”例如,但我不确定如何才能做到这一点 这是我当前的代码Swift 返回基于字符串的颜色,swift,swiftui,Swift,Swiftui,我试图根据我在结构中声明的字符串枚举选择形状的颜色。我有一个字符串“red”,我想将其转换为“Color.red”例如,但我不确定如何才能做到这一点 这是我当前的代码 // // Feature.swift // import Foundation import SwiftUI struct Feature: Equatable { private var number: Number private var shape: Shape private var shad
//
// Feature.swift
//
import Foundation
import SwiftUI
struct Feature: Equatable {
private var number: Number
private var shape: Shape
private var shading: Shading
private var color: ShapeColor
init(number: Number, shape: Shape, shading: Shading, color: ShapeColor) {
self.number = number
self.shape = shape
self.shading = shading
self.color = color
}
static func ==(first: Feature, second: Feature) -> Bool {
return first.shape == second.shape
}
func equals(compareWith: Feature) -> Bool {
return self.shape == compareWith.shape
}
func getShape(card: Feature) -> some View {
switch card.shape {
case .diamond:
return AnyView(VStack {
ForEach(0..<card.number.rawValue, id: \.self) { number in
Diamond().foregroundColor(Color.purple) // <- This is where I want to use card.color for the Color
}
})
case .squiggle:
return AnyView(VStack {
ForEach(0..<card.number.rawValue, id: \.self) { number in
Squiggle().foregroundColor(Color.red) // <- This is where I want to use card.color for the Color
}
})
case .oval:
return AnyView(VStack {
ForEach(0..<card.number.rawValue, id: \.self) { number in
Circle().foregroundColor(Color.green) // <- This is where I want to use card.color for the Color
}
})
}
}
enum Number: Int, CaseIterable {
case one = 1, two, three
}
enum Shape: String, CaseIterable {
case diamond, squiggle, oval
}
enum Shading: String, CaseIterable {
case solid, striped, open // striped will be translucent to be easier
}
enum ShapeColor: String, CaseIterable {
case red, green, purple
}
}
//
//特写:斯威夫特
//
进口基金会
导入快捷键
结构特征:equalable{
私有变量编号:编号
私有变量形状:形状
私有变量着色:着色
专用颜色:ShapeColor
初始(编号:编号,形状:形状,着色:着色,颜色:ShapeColor){
self.number=number
self.shape=shape
self.shading=着色
self.color=颜色
}
静态函数==(第一个:特征,第二个:特征)->Bool{
返回first.shape==second.shape
}
func equals(与:功能进行比较)->Bool{
返回self.shape==compareWith.shape
}
func getShape(卡片:功能)->一些视图{
开关卡形状{
案例.钻石:
返回任意视图(VStack){
ForEach(0..可以将ShapeColor枚举包装在PropertyRapper中
@propertyWrapper
enum ShapeColor: String, CaseIterable {
case red, green, purple
var wrappedValue: Color {
switch self {
case .red: return Color.red
case .purple: return Color.purple
case .green: return Color.green
}
}
}
并更改初始化
@ShapeColor private var color: Color
init(number: Number, shape: Shape, shading: Shading, color: ShapeColor) {
self.number = number
self.shape = shape
self.shading = shading
self._color = color
}
并更改getShape,将形状返回到
Circle().foregroundColor(card.color)
谢谢!这非常有效。我还是一个新手,所以我将阅读有关属性包装器的内容。一个简单的问题也许你可以帮助我。我将我的颜色属性从color重命名为ShapeColor,因为它与Apple的颜色结构混在一起。有没有办法对两者使用相同的名称,或者我做了正确的事情将其命名为ShapeColor?非常感谢!case.red:return.red
这只是我的观点,但我认为避免命名冲突比解决命名冲突更好/更容易,因为您可能同时使用您定义的类型和预定义的类型。例如,我可能想使用状态枚举来描述某个特定对象的状态ar视图,但它将与@State属性包装器冲突。因此,您对ShapeColor很在行