参考值进行数组索引

huangapple go评论90阅读模式
英文:

Refer to Array Index by Value

问题

在SwiftUI中,我有以下数据:

struct Exercise: Identifiable {
    var id = UUID()
    var name: String
    var muscleGroup: [MuscleGroup]
    var equipment: [Equipment]
}

let exercises = [
    Exercise(name: "Ab Wheel", muscleGroup: [muscleGroups[0], muscleGroups[1]], equipment: [equipments[0], equipments[2]])
]

我想通过名称而不是索引来引用muscleGroup和equipment的值,因为这对我来说更容易。我找到了解决方案,但它们只返回第一个值。

如何根据值引用索引,同时返回所有索引项而不仅仅是第一个?

我找到的解决方案是使用一个辅助函数,但这只返回第一个值:

func getEquipment(names: [String]) -> [Equipment] {
    return equipments.filter { equipment in
        names.contains(equipment.name)
    }
}

当我将该解决方案实现如下时:

let exercises = [
    Exercise(name: "Ab Wheel", muscleGroup: [muscleGroups[0], muscleGroups[1]], equipment: getEquipment(names: ["Barbell", "Dumbbell"]))
]

我得到以下结果。我希望Barbell和Dumbell都出现:

参考值进行数组索引

英文:

I have an the following data in SwiftUI.

struct Exercise: Identifiable {
    var id = UUID()
    var name: String
    var muscleGroup: [MuscleGroup]
    var equipment: [Equipment]
}

let exercises = [
    Exercise(name: "Ab Wheel", muscleGroup: [muscleGroups[0], muscleGroups[1]], equipment: [equipments[0], equipments[2]])
]

I want to refer to the muscleGroup and equipment values by name rather than index as that is easier for me. I have found solutions, however they only return the first value, rather than all values.

How can I refer to the index by value where it also returns all index items rather than only the first?

The solution I found was through a helper function, but this only returns the first value:

func getEquipment(names: [String]) -> [Equipment] {
    return equipments.filter { equipment in
        names.contains(equipment.name)
    }
}

When I implement that solution as such:

let exercises = [
    Exercise(name: "Ab Wheel", muscleGroup: [muscleGroups[0], muscleGroups[1]], equipment: getEquipment(names: ["Barbell", "Dumbbell"]))
]

I get the following result. I want both Barbell and Dumbell to appear:

参考值进行数组索引

答案1

得分: 0

以下是您要翻译的内容:

这并不完全清楚您试图做什么。我猜您想要一个易于阅读且允许列出值的类型。

我建议使用枚举:

enum MuscleGroup {
    case lats
    case glutes
    case abs
    case pecs
}

如果您需要将其视为整数以便用作数组的索引,可以将其基本类型设置为Int:

enum MuscleGroup: Int {
    case lats
    case glutes
    case abs
    case pecs
}

然后,您可以使用rawValue获取任何给定MuscleGroup的字符串描述以及从零开始的整数值:

let aMuscleGroup = MuscleGroup.lats
print("aMuscleGroup = '\(String(describing: aMuscleGroup))'")
print("aMuscleGroup int value = \(aMuscleGroup.rawValue)")

这将记录:

aMuscleGroup = 'lats'
aMuscleGroup int value = 0

您可以使用rawValue来索引数组。假设您有一个按照它们影响的肌肉群组索引的练习数组:

let latExercises = exercises[MuscleGroup.lats.rawValue]

或者您的Exercise类型:

struct Exercise: Identifiable {
    var id = UUID()
    var name: String
    var muscleGroup: [MuscleGroup]
    var equipment: [Equipment]
}

let exercises = [
    Exercise(name: "Ab Wheel", muscleGroup: [.lats, .pecs], equipment: [equipments[0], equipments[2]])
]

(并且为您的equipment类型也使用枚举将是有意义的。我将其留给您作为练习。)

英文:

It isn't completely clear what you are trying to do. I gather you want to have a type that is easy to read and lets you list values.

I suggest enums:

enum MuscleGroup {
    case lats
    case glutes
    case abs
    case pecs
}

If you need to treat it as an integer so you can use it to index into arrays, make its base type Int:

enum MuscleGroup: Int {
    case lats
    case glutes
    case abs
    case pecs
}

You can then get a string description of any given MuscleGroup as well as a zero-based integer value using rawValue:

let aMuscleGroup = MuscleGroup.lats
print(String("aMuscleGroup = '\(String(describing: aMuscleGroup))'"))
print ("aMuscleGroup int value = \(aMuscleGroup.rawValue)")

That will log

aMuscleGroup = 'lats'
aMuscleGroup int value = 0

And you can use the rawValue to index into arrays. Say you have an array of exercises indexed by the muscle groups they work:

let latExercises = exercises[MuscleGroup.lats.rawValue]

Or your Exercise type:

struct Exercise: Identifiable {
    var id = UUID()
    var name: String
    var muscleGroup: [MuscleGroup]
    var equipment: [Equipment]
}

let exercises = [
    Exercise(name: "Ab Wheel", muscleGroup: [.lats, .pecs], equipment: [equipments[0], equipments[2]])
]

(And it would make sense to use enums for your equipment type as well. I leave that as an exercise for you.)

huangapple
  • 本文由 发表于 2023年8月4日 23:25:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837302.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定