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

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。任何帮助都将不胜感激。


当前回答

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

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在字符串上的==可能并不等同于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字符串在概念上是“字符”的序列,其中“字符”是抽象扩展的字形集群(读取字符=任何数量的Unicode码点,通常是用户看到的字符和文本输入游标跳跃)。

下一个要提到的是Unicode。关于它有很多可写的,但这里我们感兴趣的是所谓的“规范等价”。使用Unicode码位,从视觉上看,相同的“字符”可以用多种方式进行编码。例如,“Á”可以表示为预先组合的“Á”或分解的a +◌(这就是为什么在示例中组合。Utf16和分解。Utf16有不同的长度)。阅读这篇很棒的文章是一件好事。

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

[Á] != [A, ◌́]

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

[ [Á] ] == [ [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 == NSString和String == String是有区别的。NSString ==将导致isEqual和UTF-16代码单元逐个代码单元的比较,其中as 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

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

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

在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为真。

使用==运算符代替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官方文档(搜索比较字符串)。