定义闭包
定义闭包
/**可以折叠 服务-特征值**/
struct RowEachItemView:View {
@Binding var arr:[YYService]
let readValueFor:(CBCharacteristic)->Void
let writeValueFor:(CBCharacteristic)->Void
var body: some View {
赋值
Button("Read"){
readValueFor(character)
}
.frame(height: 40)// 核心:指定宽高
.font(Font.system(size: 12))
.foregroundColor(.green)
.opacity(character.properties.contains(CBCharacteristicProperties.read) ? 1 : 0)
Button("Write"){
writeValueFor(character)
//TextField()
}
.frame(height: 40)// 核心:指定宽高
.font(Font.system(size: 12))
.foregroundColor(.orange)
.opacity((character.properties.contains(CBCharacteristicProperties.write)) ? 1 : 0)
使用
if !bleManager.YYContentArr.isEmpty{
//RowView
RowEachItemView(arr: $bleManager.YYContentArr){character in
// 这里是读的特征
bleManager.readCharacterValue(character)
} writeValueFor:{cha in
// 这里是写的特征
}
}
灵感来源

Swift 闭包反向传值.png