如何从枚举返回正确的类型,以便可以从示例数据中的函数使用

p1tboqfb  于 2022-10-04  发布在  Swift
关注(0)|答案(1)|浏览(136)

我试图告诉我的模型返回枚举类型的字符串,这样我就可以在前端正确地设置我的图像。我对来自CSharp背景的快速和快速的用户界面相当陌生。

struct PlayerRankingViewModel : Identifiable {
var id: UUID
var PlayerId : Int
var RankingType : Int
var RankingTypeImage : String
var FirstName:String
var LastName : String
var Reps: Decimal
var Weight: Decimal
var Seconds:Decimal
var DistanceFt:Decimal
var DistanceInches:Decimal

enum RankingTypes :Int{
    case BAH = 1
    case PU     = 2
    case TB = 3
    case BP = 4
    case OP = 5
    case PUH = 6
    case Bike = 7
    case TwentyFiveYard = 8
    case OneFiftyYard = 9
    case Jumps = 10
    case VJ = 11
    case BroadJump = 12
    case FrontSquat = 13
    case HangClean = 14
}

init(id: UUID = UUID()  ,PlayerId:Int,RankingType:Int,RankingTypeImage:String,
FirstName:String,LastName:String, Reps:Decimal,  
Weight:Decimal,Seconds:Decimal,DistanceFt:Decimal,
DistanceInches:Decimal){

    self.id = id
    self.PlayerId = PlayerId
    self.RankingType = RankingType
    self.RankingTypeImage = ConvertExerciseTypeToImage(by: RankingType)
    self.FirstName = FirstName
    self.LastName = LastName
    self.Reps = Reps
    self.Weight = Weight
    self.Seconds=Seconds
    self.DistanceFt = DistanceFt
    self.DistanceInches = DistanceInches

}

这是正在进行对话的函数

func ConvertExerciseTypeToImage(by ExerciseType: Int) -> String   {

    if(ExerciseType == RankingTypes.Bike.rawValue)
    {
        return("bicycle")

    }

}}

我的主要问题是如何在样例数据部分传递它

extension PlayerRankingViewModel {
 static var sampleData : [PlayerRankingViewModel] {
 [

   PlayerRankingViewModel(PlayerId:2,RankingType:RankingTypes.BP.rawValue,
        RankingTypeImage What Do I put here??  : ,FirstName: "Dazy",
        LastName:"Johnstone" ,Reps: 56.39, Weight:
        125.00,Seconds:0.00,DistanceFt:0.00,DistanceInches:0.00)
 ]

正如您所看到的,我不确定如何将值放入RankingTypeImage中,或者这是实现这一点的最佳方式吗?我应该只返回一个图像。我想我正在寻找打字转换器的最佳实践,我来自一个尖锐的背景。

在前端,我可以显示基于学生正在做的练习的图像?

3htmauhk

3htmauhk1#

您可以在枚举中正确地进行转换

enum RankingTypes: Int {
    case BAH = 1, PU, TB, BP, OP, PUH, Bike, TwentyFiveYard
    case OneFiftyYard, Jumps, VJ, BroadJump, FrontSquat, HangClean

    var stringValue : String {
        switch self {
            ...
            case .Bike: return "bicycle"
            ...
        }
    }
}

请考虑SWIFT的命名约定,即变量、函数和枚举大小写以小写字母开头。

在枚举中,在指定第一个整数值后将推断后续整数值

相关问题