SwiftUI struct pass param without label name All In One
SwiftUI struct pass param without label name All In One
??
// 评分
StarRatingView(snackBar: snackBar);
// infos
SnackInfoView(snackBar: snackBar);
//
// StarRatingView.swift
//
// Created by xgqfrms on 2022/5/15.
//
import SwiftUI
struct StarRatingView: View {
var snackBar: SnackBarModel;
var body: some View {
HStack(alignment: .center, spacing: 5) {
// \.slef 唯一索引 & _ 匿名参数
ForEach(1...(snackBar.rating), id: \.self) { _ in
Image(systemName: "star.fill")
.font(.body)
.foregroundColor(.yellow)
}
}
}
}
??
// 评分
StarRatingView(snackBar);
// infos
SnackInfoView(snackBar);
//
// StarRatingView.swift
//
// Created by xgqfrms on 2022/5/15.
//
import SwiftUI
struct StarRatingView: View {
var snackBar: SnackBarModel;
// ? custom init, 可以省略写 labelName
init(_ snack: SnackBarModel) {
self.snackBar = snack;
}
var body: some View {
HStack(alignment: .center, spacing: 5) {
// \.slef 唯一索引 & _ 匿名参数
ForEach(1...(snackBar.rating), id: \.self) { _ in
Image(systemName: "star.fill")
.font(.body)
.foregroundColor(.yellow)
}
}
}
}
func
// 1. labelName === paramName
// paramName: paramType
// 2. labelName 用于 func 外部传参使用, paramName 用于 func 内部使用
// labelName paramName: paramType
// 3. _ anonymous labelName, 外部使用参数无需写 labelName ?
// _ paramName: paramType
API
https://docs.swift.org/swift-book/LanguageGuide/Initialization.html#ID213
refs
4 different solutions for passing data between views
https://www.createwithswift.com/tutorial-swiftui-passing-data-between-views-using-swiftui/
https://stackoverflow.com/questions/68900579/how-to-omit-argument-name-in-a-struct
?xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有??xgqfrms, 禁止转载 ???,侵权必究??!