【问题标题】:The Convert to Latest Swift Syntax of Xcode does not convert Set related code properly. Code creates opposite resultsXcode 的 Convert to Latest Swift Syntax 不能正确转换 Set 相关代码。代码产生相反的结果
【发布时间】:2016-01-23 05:46:35
【问题描述】:

如果您一直在使用 Swift 1.2 并使用 Set(arrayLiteral:"...") 使用 Sets,则自动转换不会正确转换此类操作。过去在 Swift 1.2 中传递的内容在 Swift 2.0 中将失败

例如,如果您的 Swift 1.2 代码是

var charsSet:Set<Character> = Set("abcdefghijklmnopqrstuvwxyz_")
if charsSet.isStrictSupersetOf(password.lowercaseString)
{
    print("true")
} else {
    print("false")
}

然后转换为最新的 Swift 语法会创建此代码。

let charsSet = Set(arrayLiteral: "abcdefghijklmnopqrstuvwxyz_")
let passwordSet = Set(arrayLiteral: password.lowercaseString)
if charsSet.isStrictSupersetOf(passwordSet)
{
    print("true")
} else {
    print("false")
}

在这种情况下,结果将相反。在 Swift 1.2 中传递的内容现在将在 Swift 2.0 中失败

【问题讨论】:

    标签: swift set swift2 xcode7


    【解决方案1】:

    自动转换的代码应该改成这个。

    //Corrected code for Swift 2.0
    let charsSet2 = Set("abcdefghijklmnopqrstuvwxyz_".characters)
    let passwordSet2 = Set(password.lowercaseString.characters)
    if charsSet2.isStrictSupersetOf(passwordSet2)
    {
        print("true")
    } else {
        print("false")
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多