【问题标题】:Compilation errors in xCode 7 + Swift 2.0xCode 7 + Swift 2.0 中的编译错误
【发布时间】:2015-09-14 05:50:42
【问题描述】:

自从我安装了 xCode 7 beta2 + Swift 2.0,我的应用程序中出现了一些错误。例如,我收到以下错误

“不能使用类型为'(EKEntityType, completion: (Bool, NSError!) -> _)'的参数列表调用'requestAccessToEntityType'

在这部分代码中:

eventStore.requestAccessToEntityType(EKEntityType.Event,
    completion: {(granted: Bool, error:NSError!) in
            if !granted {
                print("Access to store not granted")
            }
    })

还有这个错误:

无法使用类型为“(NSDate,endDate:NSDate,日历:[AnyObject])”的参数列表调用“predicateForEventsWithStartDate”

在这部分代码中:

calendarsPrueba.addObject(calendarWithName("US Holidays")!)
var predicate2 = eventStore.predicateForEventsWithStartDate(startDate, endDate: endDate, calendars: calendarsPrueba as [AnyObject])

有人知道如何解决这个问题吗?没有关于此的 Apple 文档

【问题讨论】:

  • 你运行了迁移器吗?

标签: ios xcode swift swift2 xcode7-beta2


【解决方案1】:

这适用于 Xcode 7 / swift 2:

final func addToCalendar(){

    let eventStore = EKEventStore()
    eventStore.requestAccessToEntityType(EKEntityType.Event, completion: { (granted, error) in
        if !granted {
            // Show alert...
            print("Access not allowed")
            print(error!.localizedDescription)
        }
        else {
            print("Access granted")
            let event = EKEvent(eventStore: eventStore)
            let uuid = NSUUID().UUIDString
            event.title = "sample Event " + uuid
            event.startDate = NSDate();
            event.endDate = event.startDate.dateByAddingTimeInterval(60*60)
            event.calendar = eventStore.defaultCalendarForNewEvents

            do {
                try eventStore.saveEvent(event,  span: .ThisEvent)
            } catch let error as NSError {
                print(error.localizedDescription)
            }
        }
    })

【讨论】:

    【解决方案2】:

    与@HAS 相同的问题 - 您是否运行了迁移器? Swift 1.2 和 Swift 2.0 之间有很多不兼容的变化。代码必须迁移或手动修复。

    requestAccessToEntityType

    错误...

    无法使用参数列表调用“requestAccessToEntityType” type '(EKEntityType, completion: (Bool, NSError!) -> _)'

    ... 因为你的类型是(Bool, NSError!) -> Void 而不是(Bool, NSError?) -> Void。将NSError! 替换为NSError? 以修复它。

    检查文档,签名是:

    typealias EKEventStoreRequestAccessCompletionHandler = (Bool, NSError?) -> Void
    

    predicateForEventsWithStartDate

    无法使用参数列表调用“predicateForEventsWithStartDate” 类型为“(NSDate,endDate:NSDate,日历:[AnyObject])”

    签名是:

    func predicateForEventsWithStartDate(_ startDate: NSDate,
      endDate endDate: NSDate,
      calendars calendars: [EKCalendar]?) -> NSPredicate
    

    使用您的as [AnyObject],您试图传递[AnyObject] 而不是[EKCalendar]。要解决此问题,请将calendarsPrueba 声明为:

    var calendarsPrueba: [EKCalendar]
    

    并且不要将其转换为[AnyObject]

    有人知道如何解决这个问题吗?没有苹果 相关文档

    有。始终阅读发行说明,您可以在其中找到所有更改的摘要。然后重新检查文档,因为正如我所写,您会发现 Swift 1.2 和 Swift 2.0 之间有许多重大变化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多