Swift date(byAdding:to:) returns nil for trivial calculation in REPL

后端 未结 1 614
伪装坚强ぢ
伪装坚强ぢ 2020-12-12 01:23

I expect this to return a Date object representing the time one hour from now:

Calendar.current.date(b         


        
相关标签:
1条回答
  • 2020-12-12 02:10

    This is a bug in the Swift REPL, see

    • SR-11593 REPL incorrectly reports URL as nil
    • SR-12172 Description of Date? expression is printed as nil even when non-nil

    It affects both optional and non-optional values of some Swift overlay types (such as Date, Calendar, URL). Example:

    $ swift
    Welcome to Apple Swift version 5.1.3 (swiftlang-1100.0.282.1 clang-1100.0.33.15).
    Type :help for assistance.
      1> import Foundation
      2>  
      3> let d1 = Date()
    d1: Date = {}
      4> let d2: Date? = Date()
    d2: Date? = nil
      5> let url = URL(string: "https://www.google.com")
    url: URL? = nil
    

    As a workaround you can print the values:

      7> print(d1)
    2020-03-03 10:17:00 +0000
      8> print(d2)
    Optional(2020-03-03 10:17:07 +0000)
      9> print(url)
    Optional(https://www.google.com)
    
    0 讨论(0)
提交回复
热议问题