比较两个文本字段的文本

发布于 2024-11-27 15:58:11 字数 213 浏览 0 评论 0原文

如何比较两个文本字段中的文本以查看它们是否相同,例如“密码”和“确认密码”文本字段中的文本?

if (passwordField == passwordConfirmField) {

    //they are equal to each other

} else {

    //they are not equal to each other

}

How do you compare the text in two text fields to see if they are the same, such as in "Password" and "Confirm Password" text fields?

if (passwordField == passwordConfirmField) {

    //they are equal to each other

} else {

    //they are not equal to each other

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千秋岁 2024-12-04 15:58:11

在 Objective-C 中,您应该使用 isEqualToString:,就像这样:

if ([passwordField.text isEqualToString:passwordConfirmField.text]) {
    //they are equal to each other
} else {
    //they are *not* equal to each other
}

NSString 是一个指针类型。当您使用 == 时,您实际上是在比较两个内存地址,而不是两个值。字段的 text 属性是 2 个不同的对象,具有不同的地址。
因此 == 将始终1 返回 false


在 Swift 中情况有些不同。 Swift String 类型符合 Equatable 协议。这意味着它通过实现运算符 == 为您提供相等性。使以下代码可以安全使用:

let string1: String = "text"
let string2: String = "text"

if string1 == string2 {
    print("equal")
}

如果 string2 被声明为 NSString 会怎样?

let string2: NSString = "text"

由于 Swift 中 StringNSString 之间完成了一些桥接,因此 == 的使用仍然是安全的。


1:有趣的是,如果两个 NSString 对象具有相同的值,编译器可能会在后台进行一些优化并重新使用相同的对象。因此,在某些情况下可能==可能返回true。显然这不是您想要依赖的东西。

In Objective-C you should use isEqualToString:, like so:

if ([passwordField.text isEqualToString:passwordConfirmField.text]) {
    //they are equal to each other
} else {
    //they are *not* equal to each other
}

NSString is a pointer type. When you use == you are actually comparing two memory addresses, not two values. The text properties of your fields are 2 different objects, with different addresses.
So == will always1 return false.


In Swift things are a bit different. The Swift String type conforms to the Equatable protocol. Meaning it provides you with equality by implementing the operator ==. Making the following code safe to use:

let string1: String = "text"
let string2: String = "text"

if string1 == string2 {
    print("equal")
}

And what if string2 was declared as an NSString?

let string2: NSString = "text"

The use of == remains safe, thanks to some bridging done between String and NSString in Swift.


1: Funnily, if two NSString object have the same value, the compiler may do some optimization under the hood and re-use the same object. So it is possible that == could return true in some cases. Obviously this not something you want to rely upon.

伏妖词 2024-12-04 15:58:11

您可以通过使用 NSString 的 isEqualToString: 方法来做到这一点,如下所示:

NSString *password = passwordField.text;
NSString *confirmPassword = passwordConfirmField.text;

if([password isEqualToString: confirmPassword]) {
    // password correctly repeated
} else {
    // nope, passwords don't match
}

希望这会有所帮助!

You can do this by using the isEqualToString: method of NSString like so:

NSString *password = passwordField.text;
NSString *confirmPassword = passwordConfirmField.text;

if([password isEqualToString: confirmPassword]) {
    // password correctly repeated
} else {
    // nope, passwords don't match
}

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文