空格和制表符比较字符串Xcode

时间:2018-02-18 05:16:10

标签: swift xcode unit-testing tabs spaces

我写了简单的测试:

   func testTabs() {
        let tabbed = "\t"
        let spaced = "    "

        XCTAssert(tabbed == spaced, "Comparison is illegal")
    }

   func testTabs() {
        let tabbed = "\t"
        let tab = "    "

        XCTAssert(tabbed == tab, "Comparison is illegal")
    }

如果无法使用实际的' 标签'间距, 它失败了4个空格,2个空格。 因此,我将 \ t 标签进行比较,但失败了。 (也没有任何等同物)。

它应该有用吗? 如果我想进行测试,我是否应该在字符串中使用标签? 让我说明我的意思:

func generateSomeString() -> String {
    let array = ["Some","Stupid","People","Don't","Read","But","Comment"]
    let string = array.joined(separator: "\t")
    return string
}

func testStrings() {
    let string = generateSomeString()
    let expectedString = "Some    Stupid    People    Don't    Read    But    Comment"
    print(string)
    XCTAssert(string == expectedString, "Comparison is illegal")
}

1 个答案:

答案 0 :(得分:0)

标签和空格是空格,但不是一回事。

您可以将\t替换为预期的空格数,然后对其进行测试......