【问题标题】:How to requestAccessToEntityType method in Swift 2.0 iOS 9?如何在 Swift 2.0 iOS 9 中请求AccessToEntityType 方法?
【发布时间】:2023-12-29 02:37:01
【问题描述】:

我正在尝试设置提醒,需要在 Swift 2.0 for iOS9 中访问实体类型方法的请求。但是,它给了我错误:

使用未解析的标识符

@IBAction func setReminder(sender: AnyObject) {

    appDelegate = UIApplication.sharedApplication().delegate
        as? AppDelegate

    if appDelegate!.eventStore == nil {
        appDelegate!.eventStore = EKEventStore()
        appDelegate!.eventStore!.requestAccessToEntityType(EKEntityTypeReminder, completion: {(granted, error) in    //use of unresolved identifier EKEntityTypeReminder
            if !granted {
                println("Access to store not granted")
                println(error.localizedDescription)
            } else {
                println("Access granted")
            }
        })
    }

    if (appDelegate!.eventStore != nil) {
        self.createReminder()
    }
}

此代码适用于 Swift,但不适用于 Swift 2。有人遇到过此类问题吗?

【问题讨论】:

    标签: ios swift swift2 ios9 eventkit


    【解决方案1】:

    EKEntityType 现在是 enum,它包含两种可以指定的类型。

    对于EKEntityTypeReminder

    appDelegate!.eventStore!.requestAccessToEntityType(EKEntityType.Reminder, completion: 
    {(granted, error) in
        if !granted 
        {
            println("Access to store not granted")
            println(error.localizedDescription)
        }
        else 
        {
            println("Access granted")
        }
    })
    

    或者只是:

    .Reminder
    

    【讨论】: