在Swift中,如何将字符串作为类属性的名称放入函数的参数中?
我需要对一些计算进行分解,方法是输入一个属性名称的函数参数,如在Swift中,如何将字符串作为类属性的名称放入函数的参数中?,swift,function,settings,Swift,Function,Settings,我需要对一些计算进行分解,方法是输入一个属性名称的函数参数,如func totalcarrie(列表:[[Movie]],Macro:Int)->Int,例如年,在这种情况下: struct Movie { var title = "" var year = 0 var boxOffice = 0 var isImportant: Bool var isFinished: Bool init(title: String
func totalcarrie(列表:[[Movie]],Macro:Int)->Int
,例如年
,在这种情况下:
struct Movie {
var title = ""
var year = 0
var boxOffice = 0
var isImportant: Bool
var isFinished: Bool
init(title: String, year: Int, boxOffice: Int, isImportant: Bool, isFinished: Bool) {
self.title = title
self.year = year
self.boxOffice = boxOffice
self.isImportant = isImportant
self.isFinished = isFinished
}
static let list1 = [
Movie(title: "The Shawshank Redemption", year: 1, boxOffice: 2, isImportant: false, isFinished: false),
Movie(title: "The Godfather", year: 1, boxOffice: 4, isImportant: false, isFinished: false),
Movie(title: "The Dark Knight", year: 1, boxOffice: 1, isImportant: false, isFinished: false)
]
static let list2 = [
Movie(title: "The Lord of the Rings: The Fellowship of the Ring", year: 2, boxOffice: 3, isImportant: false, isFinished: false),
Movie(title: "Inception", year: 2, boxOffice: 1, isImportant: false, isFinished: false)
]
static let list2Bis = [
Movie(title: "The Lord of the Rings: The Fellowship of the Ring", year: 2, boxOffice: 4, isImportant: false, isFinished: false),
Movie(title: "Inception", year: 2, boxOffice: 6, isImportant: false, isFinished: false)
]
static let list3 = [list1, list2, list2Bis]
static let nomsDesRepas: [String] = ["Petit Déjeuner", "Collation 11h"]
}
func TotalCalorie(Liste list: [[Movie]], Macro: Int) -> Int { // It's here
var totalsection = 0
var totalcalorie = 0
for xxx in 0..<list.count {
totalsection = 0
for yyy in 0..<list[xxx].count {
totalsection += list[xxx][yyy].year
}
totalcalorie += totalsection
}
return totalcalorie
}
let calories = TotalCalorie(Liste: Movie.list3, Macro: .year)
print(calories)
struct电影{
var title=“”
风险年=0
var-boxOffice=0
变量是重要的:Bool
变量isFinished:Bool
init(标题:String,年份:Int,票房:Int,isImportant:Bool,isFinished:Bool){
self.title=标题
self.year=年
self.boxOffice=boxOffice
self.isImportant=isImportant
self.isFinished=isFinished
}
静态let列表1=[
电影(片名:《肖申克的救赎》,年份:1,票房:2,isImportant:false,isFinished:false),
电影(片名:《教父》,年份:1,票房:4,isImportant:false,isFinished:false),
电影(片名:《黑暗骑士》,年份:1,票房:1,isImportant:false,isFinished:false)
]
静态let列表2=[
电影(片名:“指环王:指环团契”,年份:2,票房:3,isImportant:false,isFinished:false),
电影(片名:《盗梦空间》,年份:2,票房:1,isImportant:false,isFinished:false)
]
静态let list2Bis=[
电影(片名:“指环王:指环团契”,年份:2,票房:4,isImportant:false,isFinished:false),
电影(片名:《盗梦空间》,年份:2,票房:6,isImportant:false,isFinished:false)
]
静态let list3=[list1、list2、list2Bis]
静态let nomsDesRepas:[String]=[“Petit Déjeuner”,“排序规则11h”]
}
func totalcarrie(列表:[[Movie]],宏:Int)->Int{//在这里
var totalsection=0
var总热量=0
对于0..中的xxx,您可以在此处尝试使用镜像
在方法中使用字符串
类型参数,并使用镜像
获取属性的值
func totalCalorie(liste list: [[Movie]], macro: String) -> Int {
var totalsection = 0
var totalcalorie = 0
list.forEach {
totalsection = 0
$0.forEach({ (movie) in
if let value = Mirror(reflecting: movie).children.first(where: { $0.label == macro })?.value as? Int {
totalsection += value
}
})
totalcalorie += totalsection
}
return totalcalorie
}
@当然,我会尽力帮忙的。