Xcode 7.3更新后的语法错误

时间:2016-03-23 07:27:01

标签: ios xcode swift

昨天我安装了新的Xcode 7.3,我的问题就开始了。

我必须做很多改动,快速3。 我通过Xcode的“自动修正”解决了许多变化。

但有两件事情无法自动解决:

问题1 enter image description here

问题2

 for ( var x = 1; x < self.ACData.count; x ++ ) {

但我应该这样做:

enter image description here

如果我应用更正,我会得到:

for ( x in 2 ..< self.ACData.count ) {

这使我出现语法错误。 有人可以帮我吗? :)

更新

let app:UIApplication = UIApplication.sharedApplication()
  for oneEvent in app.scheduledLocalNotifications! {
    let notification: AnyObject = oneEvent
    let userInfoCurrent = notification.userInfo as! [NSObject: AnyObject]
    if userInfoCurrent["UUID"] == nil {
      app.cancelLocalNotification(notification as! UILocalNotification)
    }
}

2 个答案:

答案 0 :(得分:0)

问题1:

不要垂头丧气,不是必要的。您可以输入NSObject的字符串,所以只需写下:

let userInfoCurrent = notification.userInfo!

问题2:

删除括号。这真的是你需要做的就是解决它,不要写:

for ( x in 2 ..< self.ACData.count )

这不是正确的快捷方式,应该是:

for x in 2 ..< self.ACData.count {

更新

基于上面显示的代码,我认为最好的方法是:

let app:UIApplication = UIApplication.sharedApplication()
for oneEvent in app.scheduledLocalNotifications! {
    let notification: AnyObject = oneEvent
    guard let userInfoCurrent = notification.userInfo,
          let uuid = userInfoCurrent["UUID"] else {
          app.cancelLocalNotification(notification as! UILocalNotification)        
          return //or continue, don't know if you want to keep looping after this
    }
    // Do something with UUID if it's not necessary you can update the code as follows:
    // guard let userInfoCurrent = notification.userInfo where userInfoCurrent["UUID"] != nil else {
}

我希望这有效

答案 1 :(得分:0)

问题1:

scheduledLocalNotifications明确声明为[UILocalNotification]? AnyObject的注释非常糟糕。

基本上不需要注释,因为编译器可以推断类型

let app = UIApplication.sharedApplication()
if let localNotifications = app.scheduledLocalNotifications {
    for notification in localNotifications {
      let userInfoCurrent = notification.userInfo as! [String: AnyObject]
      if userInfoCurrent["UUID"] == nil {
        app.cancelLocalNotification(notification)
      }
    }
}

或更简单

let app = UIApplication.sharedApplication()
if let localNotifications = app.scheduledLocalNotifications {
  for notification in localNotifications {
    if notification.userInfo?["UUID"] == nil {
      app.cancelLocalNotification(notification)
    }
  }
}

问题2:

与Objective-C不同,围绕if / for / while的括号中的Swift不需要这些条件,甚至可能会导致像你的情况一样的问题。

只需删除括号

即可
for x in 2 ..< self.ACData.count {