Swift添加日历事件

时间:2016-09-01 18:46:21

标签: ios swift xcode eventkit

我希望通过Xcode向日历应用添加一个事件。我看了一个针对Swift 1的教程,但是我在Swift 2中工作并且遇到了一些麻烦。这是我目前的代码:

let eventStore = EKEventStore()

// 2
switch EKEventStore.authorizationStatusForEntityType(EKEntityType.Event) {
case .Authorized:
    insertEvent(eventStore)
case .Denied:
    print("Access denied")
case .NotDetermined:
    // 3
    eventStore.requestAccessToEntityType(EKEntityType.Event, completion:
        {[weak self] (granted: Bool, error: NSError!) -> Void in
            if granted {
                self!.insertEvent(eventStore)
            } else {
                print("Access denied")
            }
        })
default:
    print("Case Default")
}

我的错误出现在这一行:

{[weak self] (granted: Bool, error: NSError!) -> Void in

有没有人对如何解决此问题有任何建议?提前谢谢。

1 个答案:

答案 0 :(得分:0)

试试这个:

let eventStore = EKEventStore()

    // 2
    switch EKEventStore.authorizationStatusForEntityType(EKEntityType.Event) {
    case .Authorized:
        insertEvent(eventStore)
    case .Denied:
        print("Access denied")
    case .NotDetermined:
        // 3
        eventStore.requestAccessToEntityType(EKEntityType.Event, completion: { [weak self](granted, error) in
            if granted {
                self?.insertEvent(eventStore)
            } else {
                print("Access denied")
            }
        })

    default:
        print("Case Default")
    }

错误应该是NSError?类型而不是NSError!