如何制作一个简单的应用程序,以天为单位显示您的年龄

时间:2014-12-04 01:52:44

标签: ios xcode swift nsdate

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var yearTextField: UITextField!
@IBOutlet weak var monthTextField: UITextField!
@IBOutlet weak var dayTextField: UITextField!

@IBOutlet weak var messageLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func daysButtonPressed(sender: AnyObject) {
    messageLabel.text = "The button is pressed!"

    var dateComponets = NSDateComponents()

    dateComponets.day = 2
    dateComponets.month = 10
    dateComponets.year = 1999

    var calendar = NSCalendar(identifier: NSGregorianCalendar )
    var birthDate = calendar?.dateFromComponents(dateComponets)
    var currentDate = NSDate.date()
}

}

我正在编译代码,突然它向我显示了这个错误:date()' is unavailable: use object construction 'NSDate()'并且它还显示了另一个错误:" ' NSCalendar&#39?;没有名为' dateFromComponents' "任何人都可以帮助我!我是swift的新手,也是堆栈溢出

2 个答案:

答案 0 :(得分:2)

使用NSDate()代替NSDate.date()

使用?替换日历之后的!

calendar!.dateFromComponents(dateComponets)

答案 1 :(得分:0)

Krys的建议是正确的,但你也遗漏了一些会给你一个问题的角色

id="Urie"

应该是:

var dateComponets = NSDateComponents()

相同
var dateComponents = NSDateComponents()

应该是

var birthDate = calendar?.dateFromComponents(dateComponets)