swiftui_swiftui中的数据验证2 0

swiftui

A look at the code you need to fully validate some data input in SwiftUI 2.0

看一下需要完全验证SwiftUI 2.0中的某些数据输入的代码

Many of the earliest virus exploits relied on poor/no data validation, indeed the problem persists even today with developers seemly forgetting to do so. 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 and put a simple example for you to copy and paste should the need arise.

许多最早的病毒利用都依赖于不良/没有数据验证,的确,即使在今天,问题仍然存在,开发人员似乎忘记了这样做。 这是一个需要避免的大漏洞,这并不难做到。 有时候正确无聊是很枯燥和耗时的。 考虑到这一点,我想我会尝试为您提供一个简单的示例,以便您在需要时进行复制和粘贴。

The requirement to input a PIN number, although it could be any string in effect. Here the initial draft.

输入PIN码的要求,尽管它可以是任何有效的字符串。 这里是初稿。

But, no wait, it’s incomplete. It doesn’t do any check 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, with 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。 当然,正如我已经提到的,您可以轻松扩展/更改它以查看其他集合,例如,如果要使用字母字符,则将其设置为“ Letter”。

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 tweeks. 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 …

还有...

Image for post
Animated GIF simple PIN data validation
动画GIF简单PIN数据验证

Add onTapGesture to the VStack [that you should change to a ZStack] that will dismiss the keyboard when you tap anywhere outside your PIN window. Above is the final code for the animated GIF you see running here.

将onTapGesture添加到VStack [您应该更改为ZStack],当您在PIN窗口之外的任何位置点击时,将关闭键盘。 上面是您在此处运行的GIF动画的最终代码。

Note I didn’t use Color.clear here because it didn’t work, nor 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 go doing something when the user has entered his/her PIN code.

我还向onEditingChanged开关的假臂添加了“执行验证”功能,以便在用户输入自己的PIN码后执行操作。

Keep calm, keep coding

保持冷静,保持编码

翻译自: https://medium.com/@marklucking/data-validation-in-swiftui-2-0-790cf6dbe49a

swiftui

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值