Xcode7、iOS9 使用未解析的标识符 'EKSpanThisEvent'

Posted

技术标签:

【中文标题】Xcode7、iOS9 使用未解析的标识符 \'EKSpanThisEvent\'【英文标题】:Xcode7, iOS9 use of unresolved identifier 'EKSpanThisEvent'Xcode7、iOS9 使用未解析的标识符 'EKSpanThisEvent' 【发布时间】:2015-08-08 14:19:20 【问题描述】:

这段代码有问题

import UIKit
import EventKit

let eventStore = EKEventStore()
    eventStore.requestAccessToEntityType(EKEntityType.Event, completion:  (success, accessError) -> Void in
        if accessError != nil 
            print("Calendar Access Error: \(accessError)")
            return
        
        let event = EKEvent(eventStore: eventStore)

        event.title = self.detailItem!.title
        event.allDay = true
        event.startDate = self.detailItem!.date
        event.endDate = self.detailItem!.date
        event.calendar = eventStore.defaultCalendarForNewEvents
        var saveToCalendarError: NSError?
        let success: Bool
        do 
            //Swift2: use of unresolved identifier 'EKSpanThisEvent'
            try eventStore.saveEvent(event, span: EKSpanThisEvent)
            success = true
         catch var error as NSError 
            saveToCalendarError = error
            success = false
         catch 
            fatalError()
        

编译器不编译,报错“use of unresolved identifier 'EKSpanThisEvent'”

希望有人能帮忙!

【问题讨论】:

【参考方案1】:

在 Swift 2 / Xcode 7 中,为了保持一致性,很多枚举都进行了更改

try eventStore.saveEvent(event, span: .ThisEvent)

【讨论】:

【参考方案2】:

如果你处理saveEvent抛出的错误会更好:

do 
    try store.saveEvent(event, span: .ThisEvent)
 catch let specError as NSError 
    print("A specific error occurred: \(specError)")
 catch 
    print("An error occurred")

更多关于 Swift 中的错误处理:https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html

【讨论】:

【参考方案3】:

对于 Swift 3

do 
   try self.eventStore.save(event, span: .thisEvent)
 catch let specError as NSError 
   print("A specific error occurred: \(specError)")
 catch 
   print("An error occurred")

【讨论】:

以上是关于Xcode7、iOS9 使用未解析的标识符 'EKSpanThisEvent'的主要内容,如果未能解决你的问题,请参考以下文章

未解析的标识符 - SKErrorPaymentCancelled [重复]

Xcode 7 beta 快速使用未解析的标识符

使用未解析的标识符“静态”

Swift:使用未解析的标识符“json”

在 xcode 7.3.1 中使用未解析的标识符“GMSPlacesClient”

Xcode 7 / iOS 9:在 SKNode 中使用 OpenGL 片段着色器未编译