【发布时间】:2020-10-24 22:50:14
【问题描述】:
粗体线(即 var text: String...)给出“不能在属性初始化程序中使用实例成员 'numberOfDevice';属性初始化程序在 'self' 可用之前运行”错误。我需要初始化吗?如果是在哪里?有不同的解决方案吗?
struct PairView: View {
var theClass = BluetoothManager()
init() {theClass.viewDidLoad()}
var body: some View {
List {
ForEach(0..<BluetoothManager.peripheralArray.count) { number in //iterates thru 0 to array's count
ConnectionView(numberOfDevice: number) // create a ConnectionView for each number
}
}
}
}
//-------
struct ConnectionView: View {
var numberOfDevice: Int
**var text: String = (BluetoothManager.peripheralArray[numberOfDevice]?.name)!**
// 'name' is a String property of the B.M. class's array's 'numberOfDevice index.'
var body: some View {
ZStack{
RoundedRectangle(cornerRadius: 10.0).fill(Color.blue)
Text(text).foregroundColor(Color.black)
}
}
}
【问题讨论】:
-
您能否提供有关您的 BluetoothManager 和 peripheralArray 的更多信息,其中存储了哪些对象?该对象可以识别吗?
标签: swiftui