在Swift 4中创建简单的倒计时日期应用程序时出错

时间:2018-12-04 10:19:51

标签: swift

因此,我目前正在开发一款将对特定日期进行简单倒计时的应用;但是,我收到此错误:“类型'((NSCalendar.Unit,Date,Date [NSCalendar.Options])->()'的值没有成员'day'“

有什么办法解决此问题以完成应用程序?

class ViewController: UIViewController {
    @IBOutlet weak var CountdownText: UILabel!
    let formatter = DateFormatter()
    let userCalendar = NSCalendar.current

    let requestedComponent: NSCalendar.Unit = [
        NSCalendar.Unit.month,
        NSCalendar.Unit.day,
        NSCalendar.Unit.hour,
        NSCalendar.Unit.minute,
        NSCalendar.Unit.second,
    ]

    func printTime()
    {
        formatter.dateFormat = "MM/dd/yy hh:mm:ss a"
        let startTime = NSDate()
        let endTime = formatter.date(from: "12/03/18 2:00:00 p")

        func timeDifference (requestedComponent: NSCalendar.Unit, from: Date, to: Date, options: [NSCalendar.Options]) {}

        CountdownText.text = "\(timeDifference.day) Days \(timeDifference.minute) Minutes \(timeDifference.second) Seconds"
    }
}

4 个答案:

答案 0 :(得分:1)

您正在尝试从.day类型获得调用function属性,该属性不会返回任何内容。 我敢打赌,您的函数timeDifference应该在printTime函数之外。 相信您想要执行以下操作:

func printTime() {
    formatter.dateFormat = "MM/dd/yy hh:mm:ss a"
    let startTime = Date()
    let endTime = formatter.date(from: "12/03/18 2:00:00 p")

    let days = timeDifference(requestedComponent: .day, from: startTime, to: endTime, options: [])
    let minutes = timeDifference(requestedComponent: .minute, from: startTime, to: endTime, options: [])
    let seconds = timeDifference(requestedComponent: .second, from: startTime, to: endTime, options: [])
    CountdownText.text = "\(days) Days \(minutes) Minutes \(seconds) Seconds"
}

func timeDifference (requestedComponent: NSCalendar.Unit, from: Date, to: Date, options: [NSCalendar.Options]) -> Int {
    var calculatedValue = 0
    // calculatings
    return calculatedValue
}

答案 1 :(得分:0)

您已经声明了一个名为timeDifference()的函数,并试图像变量实例一样访问它

这是您需要做的:-

 func timeDifference (requestedComponent: NSCalendar.Unit, from: Date, to: Date, options: [NSCalendar.Options]) {
     //Do your things here
 }

并以此方式在变量内部调用函数:-

 var text = "\(timeDifference(requestedComponent: .day, from: Date(), to: Date(), options: [.searchBackwards])) Days \(timeDifference(requestedComponent: .minute, from: Date(), to: Date(), options: [.searchBackwards])) Minutes \(timeDifference(requestedComponent: .second, from: Date(), to: Date(), options: [.searchBackwards])) Seconds"

此外,您可以将函数参数设置为可选,如下所示:-

 func timeDifference (requestedComponent: NSCalendar.Unit, from: Date, to: Date, options: [NSCalendar.Options]? = nil) {}

这里的第四个参数是可选的,您不必每次都提供一个选项。它被认为是无输入的nil,并且不会以任何方式影响您,除非您在函数内的某个地方使用它,否则您可能需要使用“ if”语句来处理它 (以防万一!)

注意:-

您还可以从函数中返回一个“ NSCalendar.Unit”并将其用于计算,在这种情况下,您的使用模式是正确的。

但是我假设这里的函数是出于计算目的而编写的,并且您正在处理标签的值以显示计算结果。在函数中处理所有这些是函数的唯一目的,因此,我建议这样做

答案 2 :(得分:0)

您正试图将函数(无返回类型)用作NSCalendarComponent,因此它向您显示错误。相反,您将获得像这样的开始时间和结束时间组件:

func printTime(){
    formatter.dateFormat = "MM/dd/yy hh:mm:ss a"
    let calendar = Calendar.current
    let startTime=calendar.dateComponents([.hour,.minute,.second], from: Date())
    print("\(startTime.hour!):\(startTime.minute!):\(startTime.second!)")

    let endTime = calendar.dateComponents([.hour,.minute,.second], from: formatter.date(from: "12/03/18 2:00:00 p") ?? Date())
    print("\(endTime.hour!):\(endTime.minute!):\(endTime.second!)")

   //do calculations here for time difference
}

现在您可以计算时差并将其显示在倒计时文本上。

答案 3 :(得分:0)

首先在Swift 3+中根本不使用NSCalendarNSDate

您的代码不起作用,因为函数timeDifference没有返回值,反正什么也不做。

您可能是说

let requestedComponents: Set<Calendar.Component> = [.month, .day, .hour, .minute, .second]

...

func timeDifference (requestedComponents: Set<Calendar.Component>, from: Date, to: Date) -> DateComponents {
    return Calendar.current.dateComponents(requestedComponents, from: from, to: to)
}

... 

let difference = timeDifference(requestedComponents: requestedComponents, from: startTime, to: endTime)
CountdownText.text = "\(difference.day!) Days \(difference.minute!) Minutes \(difference.second!) Seconds"

DateComponentsFormatter

是一种更方便的方法来计算和显示时差。
func printTime()
{
    let formatter = DateFormatter()
    formatter.dateFormat = "MM/dd/yy hh:mm:ss a"
    let startTime = Date()
    let endTime = formatter.date(from: "12/03/18 2:00:00 p")!
    let componentsFormatter = DateComponentsFormatter()
    componentsFormatter.allowedUnits = [.hour, .minute, .second]
    componentsFormatter.unitsStyle = .full
    CountdownText.text = componentsFormatter.string(from: startTime, to: endTime)
}