#SwiftUI ForEach エラーについて質問です。
SwiftUIを勉強し始めた者です。
ロック画面を制作しているのですがForEachのFに赤い線が引かれ下記のエラーが出ます。
####[エラー内容]
Generic parameter 'Data' could not be inferred
Explicitly specify the generic arguments to fix this issue
どうすればこのエラーは消えるのでしょうか?
教えてください。よろしくお願い致します。
SwiftUI
1 2public struct Lock: View { 3 4var maxDigits: Int = 4 5var label = "Enter One Time Password" 6 7@State var pin: String = "" 8@State var showPin = false 9@State var isDisabled = false 10 11var handler: (String, (Bool) -> Void) -> Void 12 13public var body: some View { 14VStack(spacing: 30) { 15Text(label).font(.title) 16ZStack { 17pinDots 18backgroundField 19} 20showPinStack 21} 22 23} 24 25private var pinDots: some View { 26HStack { 27Spacer() 28 29//エラー箇所 30 31ForEach(0..<maxDigits) { index in 32Image(systemName: self.getImageName(at: index)) 33.font(.system(size:30, weight:30, design: .default)) 34Spacer() 35} 36} 37} 38 39private var backgroundField: some View { 40let boundPin = Binding<String>(get: { self.pin }, set: { newValue in 41self.pin = newValue 42self.submitPin() 43}) 44 45return TextField("", text: boundPin, onCommit: submitPin) 46 47// Introspect library can used to make the textField become first resonder on appearing 48// if you decide to add the pod 'Introspect' and import it, comment #50 to #53 and uncomment #55 to #61 49 50.accentColor(.clear) 51.foregroundColor(.clear) 52.keyboardType(.numberPad) 53.disabled(isDisabled) 54 55// .introspectTextField { textField in 56// textField.tintColor = .clear 57// textField.textColor = .clear 58// textField.keyboardType = .numberPad 59// textField.becomeFirstResponder() 60// textField.isEnabled = !self.isDisabled 61// } 62} 63 64private var showPinStack: some View { 65HStack { 66Spacer() 67if !pin.isEmpty { 68showPinButton 69} 70} 71.frame(height:30) 72.padding([.trailing]) 73} 74 75private var showPinButton: some View { 76Button(action: { 77self.showPin.toggle() 78}, label: { 79self.showPin ? 80Image(systemName: "eye.slash.fill").foregroundColor(.primary) : 81Image(systemName: "eye.fill").foregroundColor(.primary) 82}) 83} 84 85private func submitPin() { 86guard !pin.isEmpty else { 87showPin = false 88return 89} 90 91if pin.count == maxDigits { 92isDisabled = true 93 94handler(pin) { isSuccess in 95if isSuccess { 96print("pin matched, go to next page, no action to perfrom here") 97} else { 98pin = "" 99isDisabled = false 100print("this has to called after showing toast why is the failure") 101} 102} 103} 104 105// this code is never reached under normal circumstances. If the user pastes a text with count higher than the 106// max digits, we remove the additional characters and make a recursive call. 107if pin.count > maxDigits { 108pin = String(pin.prefix(maxDigits)) 109submitPin() 110} 111} 112 113private func getImageName(at index: Int) -> String { 114if index >= self.pin.count { 115return "circle" 116} 117 118if self.showPin { 119return self.pin.digits[index].numberString + ".circle" 120} 121 122return "circle.fill" 123} 124} 125 126extension String { 127 128var digits: [Int] { 129var result = [Int]() 130 131for char in self { 132if let number = Int(String(char)) { 133result.append(number) 134} 135} 136 137return result 138} 139 140} 141 142extension Int { 143 144var numberString: String { 145 146guard self < 10 else { return "0" } 147 148return String(self) 149} 150}
回答1件
あなたの回答
tips
プレビュー