2014-12-04 64 views
-1
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’「沒有名爲'dateFromComponents'的會員「,任何人都可以幫助我!我是swift新手,也是堆棧溢出如何使一個簡單的應用程序,顯示在天

+0

應該有人更改標題,也許移動文本上面的代碼中的一段,但這個問題不應該被關閉。 – 2014-12-04 03:14:08

回答

2

使用NSDate()而不是NSDate.date()

!代替日曆後的?

calendar!.dateFromComponents(dateComponets)

0

Krys的建議是正確的,但你也留下了一些字符,會給你一個問題

var dateComponets = NSDateComponents() 

應該是:

var dateComponents = NSDateComponents() 

同樣的,

var birthDate = calendar?.dateFromComponents(dateComponets) 

var birthDate = calendar?.dateFromComponents(dateComponents) 
相關問題