期待','分隔符和未解析的标识符

时间:2015-12-08 21:27:37

标签: ios swift

我现在已经找了一天这些错误的解决方案,我不知道如何解决它们。下面的代码收到多条错误消息:

  

使用未解析的标识符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)

2 个答案:

答案 0 :(得分: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 :(得分:1)

这是因为您尝试在Swift中执行Objective-C代码。

只需用

替换该行
localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1

当您看到类似此[ClassName classMethod]或此*variable或类似内容的内容时,它就是Objective-C。 []表示您向对象发送消息。在Swift中,您只需使用()调用方法:ClassName.classMethod()