搜索
您的当前位置:首页正文

Swift5新特性 & XCode 10.2更新

来源:二三娱乐

swift语法更新

  1. 原始字符串
    添加了创建原始字符串的功能,其中\""被解释为这些文字符号本身,而不是转义字符或字符串终止符。这使得许多功能更加容易实现,比如正则表达式。
    要使用原始字符串,请在字符串前放置一个或多个#号,如下所示:
//正则表达式 regex1 == regex2
    let regex1 = "\\\\[A-Z]+[A-Za-z]+\\.[a-z]+"
    let regex2 = #"\\[A-Z]+[A-Za-z]+\.[a-z]+"#
    
 let keypaths = #"Swift 中的 keypaths 格式像这样: \Person.name ."#
 
 //原始字符串中包含#
 let anotherString = ##"这是一个包含“#”的原始字符串"##
 //多行字符串的原始字符串
 let multiline = #"""
    这是一个多行字符串:,
    “我才是
    多行字符串”。
    """#
 
 //原始字符串中插值
  let answer = 42
    let dontpanic = #"宇宙的终极答案是:\#(answer)."#
    

请注意,我如何使用\(answer)来使用字符串插值\(answer)将被解释为字符串中的字符,因此当您希望在原始字符串中进行字符串插值时,必须添加额外的`#'

struct User {
    var name: String
    var age: Int
}
extension String.StringInterpolation {
    mutating func appendInterpolation(_ value: User) {
        appendInterpolation("My name is \(value.name) and I'm \(value.age)")
    }
}

let user = User(name: "Guybrush Threepwood", age: 33)
 print("User details: \(user)")
 //输出:User details: My name is Guybrush Threepwood and I'm 33
 
 //格式化插值到字符串
extension String.StringInterpolation {
    mutating func appendInterpolation(_ number: Int, style: NumberFormatter.Style) {
        let formatter = NumberFormatter()
        formatter.numberStyle = style

        if let result = formatter.string(from: number as NSNumber) {
            appendLiteral(result)
        }
    }
}

 let number = Int.random(in: 0...100)
  let lucky = "The lucky number this week is \(number, style: .spellOut)."
 print(lucky)
 //输出:The lucky number this week is sixty-three.

在此基础上用户可以扩展更多类型的差之方法如:输出小数位数,控制电话号码格式,邮件格式等等,更多例子可以查看下方whats-new-in-swift-5-0

注意:旧_ExpressibleByStringInterpolation协议已被删除; 任何使用此协议的代码都需要针对新设计​​进行更新。一个#if compiler块可用于条件化4.2和5.0之间的代码,例如:

#if compiler(<5.0)
extension MyType : _ExpressibleByStringInterpolation { ... }
#else
extension MyType : ExpressibleByStringInterpolation { ... }
#endif
enum PasswordError: Error {
        case short
        case obvious
        case simple
    }
    //这个方法没有任何提示
    func showOld(error: PasswordError) {
        switch error {
        case .short:
            print("Your password was too short.")
        case .obvious:
            print("Your password was too obvious.")
        default:
            print("Your password was too simple.")
        }
    }
    
    func showNew(error: PasswordError) {
        switch error { //此行警告⚠️Switch must be exhaustive
        case .short:
            print("Your password was too short.")
        case .obvious:
            print("Your password was too obvious.")
        @unknown default:
            print("Your password wasn't suitable.")
        }
    }
struct User {
    var id: Int
    init?(id: Int) {
        if id < 1 {
            return nil
        }
        self.id = id
    }
    func getMessages() throws -> String {
        // complicated code here
        return "No messages"
    }
}

let user = User(id: 1)
let messages = try? user?.getMessages()

在swift4.2中上方代码中messages的类型将会是一个String??类型,在swift5中你会得到一个String?类型,这意味着,链式调用不会再使可选值发生嵌套。

    let rowNumber = 4
    if rowNumber.isMultiple(of: 2) {
        print("Even")
    } else {
        print("Odd")
    }
 let times = [
        "Hudson": "38",
        "Clarke": "42",
        "Robinson": "35",
        "Hartis": "DNF"
    ]
    //将[String:String]转换成[String:Int]
 let finishers1 =  { Int($0) }
  let finishers2 = 
  // ["Hudson": 38, "Clarke": 42, "Robinson": 35]
struct Q: ExpressibleByStringLiteral {
  typealias StringLiteralType =  String
  var question: String

  init?(_ possibleQuestion: StringLiteralType) {
    return nil
  }
  init(stringLiteral str: StringLiteralType) {
    self.question = str
  }
}

_ = Q("ultimate question")    // 'nil'
_ = "ultimate question" as Q  // Q(question: 'ultimate question')
func foo(_ fn: @autoclosure () -> Int) {}
func bar(_ fn: @autoclosure () -> Int) {
  foo(fn)   // ❌ `fn` can't be forwarded and has to be called
  foo(fn()) // ✅
}
class Base {
  class func factory() -> Self { ... }
}

class Derived : Base {
  class override func factory() -> Derived { ... }
}
protocol MyView : UIView { ... }
protocol MyView where Self : UIView { ... }

请注意,Swift 4.2接受了第二种形式,但它没有完全实现,有时可能会在编译时或运行时崩溃。

swift5适配遇到的问题

pod 'HandyJSON', git:  , branch: 'dev_for_swift5.0'

XCode 10.2更新


  1. Interface Builder:双击故事板不再缩放。使用触控板上的捏合手势进行缩放或按住Option并滚动。
  2. LLDB调试:可以使用LLDB调试在闭包内的表达式$0,$1......
  3. LLDB现在支持C的可变长数组
  4. LLDB调试器有一个新的命令别名,v用于“帧变量”命令,用于在当前堆栈帧中打印变量。因为它绕过表达式评估器,v比p或po优先级更高,并且速度更快
  5. Xcode使用SSH配置来确定应该使用哪个SSH密钥来验证远程存储库。
  6. 更多内容浏览下方文档Xcode 10.2 Release Notes

参考文档

Top