在Objective-C中等价的isEqualToString是什么?

我正在尝试运行下面的代码:

import UIKit


class LoginViewController: UIViewController {


@IBOutlet var username : UITextField = UITextField()
@IBOutlet var password : UITextField = UITextField()


@IBAction func loginButton(sender : AnyObject) {


if username .isEqual("") || password.isEqual(""))
{
println("Sign in failed. Empty character")
}
}

我之前的代码是在Objective-C中编写的,运行得很好:

 if([[self.username text] isEqualToString: @""] ||
[[self.password text] isEqualToString: @""] ) {

我假设我不能在Swift中使用isEqualToString。任何帮助都将不胜感激。

266673 次浏览

在Swift中,==操作符相当于Objective C的isEqual:方法(它调用isEqual方法而不仅仅是比较指针,并且有一个新的===方法来测试指针是否相同),所以你可以这样写:

if username == "" || password == ""
{
println("Sign in failed. Empty character")
}

在Swift中,你不再需要检查isEqualToString是否相等

你现在可以使用==

例子:

let x = "hello"
let y = "hello"
let isEqual = (x == y)

现在isEqual是true

使用= =操作符代替isEqual

比较字符串

Swift提供了三种比较字符串值的方法:字符串相等、前缀相等和后缀相等。

字符串平等

如果两个String值以相同的顺序包含完全相同的字符,则认为它们相等:

let quotation = "We're a lot alike, you and I."
let sameQuotation = "We're a lot alike, you and I."
if quotation == sameQuotation {
println("These two strings are considered equal")
}
// prints "These two strings are considered equal"
.
.
.

更多信息请阅读Swift官方文档(搜索比较字符串)

在Swift中isEmpty函数将检查字符串是否为空。

 if username.isEmpty || password.isEmpty {
println("Sign in failed. Empty character")
}

我添加到@JJSaccolo答案,你可以创建自定义equals方法作为新的字符串扩展,如:

extension String {
func isEqualToString(find: String) -> Bool {
return String(format: self) == find
}
}

和用法:

let a = "abc"
let b = "abc"


if a.isEqualToString(b) {
println("Equals")
}

当然,原始操作符==可能更好(在Javascript中工作),但对我来说,isEqual方法提供了一些代码的清晰度,我们比较字符串

希望对大家有所帮助,

实际上,swift似乎在试图让字符串更像值,而不是对象。然而,这并不意味着在底层swift不把字符串作为对象,我相信你们都注意到了,你仍然可以调用字符串的方法并使用它们的属性。

例如:-

//example of calling method (String to Int conversion)
let intValue = ("12".toInt())
println("This is a intValue now \(intValue)")




//example of using properties (fetching uppercase value of string)
let caUpperValue = "ca".uppercaseString
println("This is the uppercase of ca \(caUpperValue)")

在objectC中,你可以在调用方法的基础上,通过变量传递对字符串对象的引用,这基本上确立了字符串是纯对象的事实。

当你试图将字符串视为对象时,在swift中,你不能通过引用通过变量传递字符串对象。Swift总是会传递一个全新的字符串副本。因此,字符串在swift中通常被称为值类型。事实上,两个字符串字面量不会完全相同(===)。它们被视为两个不同的副本。

let curious = ("ca" === "ca")
println("This will be false.. and the answer is..\(curious)")

正如你所看到的,我们开始摆脱将字符串视为对象的传统方式,而将它们视为值。因此,. isequaltostring作为字符串对象的标识操作符不再有效,因为你永远无法在Swift中获得两个相同的字符串对象。您只能比较它的值,换句话说,检查是否相等(==)。

 let NotSoCuriousAnyMore = ("ca" == "ca")
println("This will be true.. and the answer is..\(NotSoCuriousAnyMore)")

当您查看swift中字符串对象的可变性时,这变得更加有趣。但这是另一个问题,另一天。你应该研究一下,因为这很有趣。:)希望这能澄清一些困惑。干杯!

对于UITextField文本比较,我正在使用下面的代码和工作为我很好,让我知道,如果你发现任何错误。

if(txtUsername.text.isEmpty || txtPassword.text.isEmpty)
{
//Do some stuff
}
else if(txtUsername.text == "****" && txtPassword.text == "****")
{
//Do some stuff
}

重要的一点是Swift在字符串上的==可能不等同于Objective-C的-isEqualToString:。其特点在于Swift和Objective-C之间字符串的表示方式不同。

看看这个例子:

let composed = "Ö" // U+00D6 LATIN CAPITAL LETTER O WITH DIAERESIS
let decomposed = composed.decomposedStringWithCanonicalMapping // (U+004F LATIN CAPITAL LETTER O) + (U+0308 COMBINING DIAERESIS)


composed.utf16.count // 1
decomposed.utf16.count // 2


let composedNSString = composed as NSString
let decomposedNSString = decomposed as NSString


decomposed == composed // true, Strings are equal
decomposedNSString == composedNSString // false, NSStrings are not

NSString表示为UTF-16代码单元序列(大致读取为UTF-16(固定宽度)代码单元数组)。鉴于Swift __abc1在概念上是"Characters"的序列,其中"Character"是抽象扩展字素簇的东西(读取字符=任意数量的Unicode代码点,通常是用户看到的字符和文本输入游标在周围跳跃的东西)。

下一个要提到的是Unicode。关于它有很多可写的东西,但这里我们感兴趣的是所谓的“规范等效”。使用Unicode码位,视觉上相同的"字符"可以用多种方式进行编码。例如,"Á"可以表示为预先组成的"Á"或分解为A +◌*(这就是为什么在例子中composed.utf16decomposed.utf16有不同的长度)。这篇很棒的文章是一个值得阅读的内容。

-[NSString isEqualToString:],根据文档,逐个代码单元比较nsstring代码,因此:

[Á] != [A, ◌́]

Swift的字符串== 根据规范等价性比较字符

[ [Á] ] == [ [A, ◌́] ]

在swift中,上面的例子将为Strings返回true。这就是为什么-[NSString isEqualToString:]不等同于Swift的String ==。等价的纯Swift比较可以通过比较String的UTF-16视图来完成:

decomposed.utf16.elementsEqual(composed.utf16) // false, UTF-16 code units are not the same
decomposedNSString == composedNSString // false, UTF-16 code units are not the same
decomposedNSString.isEqual(to: composedNSString as String) // false, UTF-16 code units are not the same

另外,在Swift中NSString == NSStringString == String是有区别的。NSString ==将导致isEqual和UTF-16代码单元逐个代码单元的比较,其中String ==将使用规范等价:

decomposed == composed // true, Strings are equal
decomposed as NSString == composed as NSString // false, UTF-16 code units are not the same

整个例子是:

let composed = "Ö" // U+00D6 LATIN CAPITAL LETTER O WITH DIAERESIS
let decomposed = composed.decomposedStringWithCanonicalMapping // (U+004F LATIN CAPITAL LETTER O) + (U+0308 COMBINING DIAERESIS)


composed.utf16.count // 1
decomposed.utf16.count // 2


let composedNSString = composed as NSString
let decomposedNSString = decomposed as NSString


decomposed == composed // true, Strings are equal
decomposedNSString == composedNSString // false, NSStrings are not


decomposed.utf16.elementsEqual(composed.utf16) // false, UTF-16 code units are not the same
decomposedNSString == composedNSString // false, UTF-16 code units are not the same
decomposedNSString.isEqual(to: composedNSString as String) // false, UTF-16 code units are not the same