swiftui
Many of the earliest virus exploits relied on poor/no data validation. Indeed, the problem persists even today with developers seemingly forgetting about data validation. This is a big hole that needs to be avoided, which isn’t difficult to do. It’s just rather dull and time-consuming to get right sometimes.
许多最早的病毒利用都是依靠不良/没有数据验证。 实际上,即使在今天,问题仍然存在,开发人员似乎忘记了数据验证。 这是一个需要避免的大漏洞,这并不难做到。 有时候正确无聊是很乏味和费时的。
With that in mind, I thought I would try to provide a simple example for you to copy and paste should the need arise. The requirement is to input a PIN number, although it could be any string in effect.
考虑到这一点,我想我会在需要时尝试提供一个简单的示例,供您复制和粘贴。 要求是输入PIN码,尽管它可以是任何有效的字符串。
Here’s the initial draft:
这是初稿:
import SwiftUI struct ContentView: View { @State private var name: String = "PIN" var body: some View { VStack { TextField("Enter your name:", text: $name) .textFieldStyle(RoundedBorderTextFieldStyle()) .onChange(of: name) { newValue in if !(name.last?.isNumber ?? true) { name.removeLast() } } .padding() } } }But wait, it’s incomplete. It doesn’t do any check for the number of characters you enter. To do that, you simply have to add one more check within the onChange block:
但是,等等,这还不完整。 它不会检查您输入的字符数。 为此,您只需要在onChange块中再添加一个检查:
if name.count > 6 { name.removeLast()}This will limit our input to just six digits, which seems like a reasonable PIN to me. Of course, as I already mentioned, you can extend/change this easily to look at a different set. Make it isLetter if you want alphabetic characters, for example.
这会将我们的输入限制为只有六位数,这对我来说似乎是一个合理的PIN。 当然,正如我已经提到的,您可以轻松扩展/更改它以查看其他集合。 让它isLetter如果你想字母字符,例如。
But before you go, I think we can do a little more. Add the modifiers here to change the keyboard type to a number pad, the alignment to centre, and the frame to make it fit better:
但在您走之前,我认为我们可以做更多的事情。 在此处添加修饰符可将键盘类型更改为数字键盘,将对齐方式更改为居中,并更改框架以使其更合适:
.keyboardType(.numberPad).multilineTextAlignment(.center).frame(width: 128, height: 32)And change the TextField to look like this to replace the word PIN when it takes focus:
并更改TextField使其看起来像这样,以在获得焦点时替换PIN字:
TextField("Enter your name:", text: $name, onEditingChanged: { (editingChanged) in if editingChanged { if name == "PIN" { name = "" } } })Just a few more tweaks. Add an alert to make sure the message comes over that letters are not what you needed here by adding an alert view modifier to the VStack itself:
再进行一些调整。 通过向VStack本身添加警报视图修饰符,添加警报以确保消息通过,以确保字母不是您所需要的字母:
.alert(isPresented: $showAlert) { Alert(title: Text("Important message"), message: Text("Numbers Only"), dismissButton: .default(Text("Ok")))}And:
和:
Animated GIF of simple PIN data validation 简单PIN数据验证的动画GIFAdd an onTapGesture to the VStack (that you should change to a ZStack) that will dismiss the keyboard when you tap anywhere outside your PIN window. This is the final code for the animated GIF you see running above:
将onTapGesture添加到VStack (应更改为ZStack ),当您在PIN窗口之外的任何位置点击时,将关闭键盘。 这是您在上面看到的GIF动画的最终代码:
struct ContentView: View { @State private var name: String = "PIN" @State private var showAlert = false var body: some View { ZStack { Color.yellow .opacity(0.01) .onTapGesture { UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to:nil, from:nil, for:nil) } TextField("PIN", text: $name, onEditingChanged: { (editingChanged) in if editingChanged { if name == "PIN" { name = "" } } else { print("go verify") } } ) .textFieldStyle(RoundedBorderTextFieldStyle()) .onChange(of: name) { newValue in if !(name.last?.isNumber ?? true) { name.removeLast() showAlert = true } if name.count > 6 { name.removeLast() } } .padding() .keyboardType(.numberPad) .multilineTextAlignment(.center) .frame(width: 128, height: 32) }.alert(isPresented: $showAlert) { Alert(title: Text("Important message"), message: Text("Numbers Only"), dismissButton: .default(Text("Ok"))) } } }I didn’t use Color.clear here because it didn’t work. Neither did Color.yellow with a zero opacity. What I did looks like the best compromise.
我没有在这里使用Color.clear ,因为它没有用。 不带零不透明度的Color.yellow也没有。 我所做的事情似乎是最好的折衷方案。
I also added a “go verify” to the false arm of the onEditingChanged switch to do something when the user has entered their PIN code.
我还向onEditingChanged开关的假臂添加了“go verify”以便在用户输入PIN码后执行某些操作。
Keep calm, keep coding.
保持冷静,保持编码。
翻译自: https://medium.com/better-programming/data-validation-in-swiftui-2-0-790cf6dbe49a
swiftui