2015-12-08 75 views
0

我已經看過了這些錯誤了一天的解決方案,現在,我不知道如何解決這些問題。下面的代碼接收多個錯誤消息:預計「」分隔符和未解決的標識符

使用在線路未解決標識符NSCalendarUnitDay

的:

localNotification.repeatInterval = NSCalendarUnitDay 

和三個錯誤:

  • 預期」, '分隔符
  • 使用未解決的識別符 'sharedApplication'
  • 使用未解決的識別符的 'applicationBadgeNumber'

在行:

localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; 

這是整個代碼:

override func viewDidLoad() { 
    super.viewDidLoad() 


    var localNotification:UILocalNotification = UILocalNotification() 
    localNotification.fireDate = datePicker.date 
    localNotification.alertBody = nil; 
    localNotification.alertAction = nil; 
    localNotification.repeatInterval = NSCalendarUnitDay 

    //Add one to the icon badge number 

    localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; 


    UIApplication.sharedApplication().scheduleLocalNotification(localNotification) 
+0

好了,代碼仍然是一半的Objective-C。 – Sulthan

+0

從Objective-C代碼複製&粘貼[[UIApplication sharedApplication] applicationIconBadgeNumber]'? – DevAndArtist

+0

任何想法如何我可以重寫它,或者我可以讀到它是如何完成的? – Gjeruldsen

回答

2

這是非常基本的。您可以將代碼更改爲:

override func viewDidLoad() { 
    super.viewDidLoad() 

    var localNotification:UILocalNotification = UILocalNotification() 
    localNotification.fireDate = datePicker.date 
    localNotification.alertBody = nil; 
    localNotification.alertAction = nil; 
    localNotification.repeatInterval = NSCalendarUnit.Day 

    //Add one to the icon badge number 

    localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1; 


    UIApplication.sharedApplication().scheduleLocalNotification(localNotification) 

} 
1

發生這種情況是因爲您嘗試在Swift中執行Objective-C代碼。

只需更換該行以

localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1 

當你看到這樣的事情[ClassName classMethod]或本*variable或類似的東西它是Objective-C的。而[]意味着你發送消息給對象。在斯威夫特只需調用方法與()ClassName.classMethod()