我有这个奇怪的错误。我有一个名为Bool
的全球CalendarAccess
:
var EventStore: EKEventStore!
var Calendar: NSCalendar!
var CalendarAccessRequestComplete = false
var _CalendarAccess = false
var CalendarAccess: Bool {
set(value)
{
_CalendarAccess = value;
}
get {
return _CalendarAccess;
}
}
正如你所看到的,我为它做了一个setter和getter所以我可以设置一个断点来查看它的设置位置。我这样做了,每次遇到断点时,值都是true
。我确定我从未直接设置_CalendarAccess
因为此变量直到刚才被称为CalendarAccess
。
但是,当我在视图控制器中执行以下操作时,CalendarAccess
为false
!
@IBAction func saveEvent(_ sender: Any) {
if(CalendarAccess)
{
let event = EKEvent(eventStore: EventStore)
event.title = "My Event"
event.startDate = Date();
event.endDate = Calendar.date(byAdding: .hour, value: 1, to: Date(), options: .matchFirst)!
event.notes = "A note"
event.calendar = EventStore.defaultCalendarForNewEvents
do
{
try EventStore.save(event, span: .thisEvent)
} catch
{
print("Unable to save event")
}
}
else
{
ErrorAlert(title: "No calendar access", text: "Please give the app access to your calendar. You can do that in your iOS device's settings.")
}
}
我不知道这是怎么可能的 - 变量是一个与任何视图或控制器无关的全局变量。
最后一个代码块来自的控制器以模态方式呈现,如果该信息完全有用的话。
编辑: CalendarAccess
仅在一个地方设置(AppDelegate):
func updateCalendarAccess()
{
Calendar = NSCalendar.current as NSCalendar!
CalendarAccessRequestComplete = false
EventStore = EKEventStore()
EventStore.requestAccess(to: .event, completion: {
(access: Bool, e: Error?) in
CalendarAccessRequestComplete = true
CalendarAccess = access;
return
})
while(!CalendarAccessRequestComplete) { }
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
// Override point for customization after application launch.
updateCalendarAccess()
return true
}
编辑:当我点按调用@IBAction func saveEvent
的按钮时收到这些消息:
2017-02-17 13:58:00.980237 Calendar[16695:5163026] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2017-02-17 13:58:00.989165 Calendar[16695:5163026] [MC] Reading from public effective user settings.
编辑:当我关闭模态显示的视图控制器(具有saveEvent
func的视图控制器)并记录CalendarAccess
的值时,它再次成立。并且没有命中setter断点。
编辑:当我呈现VC时,似乎_CalendarAccess
的值会回到初始值。如果我将var _CalendarAccess = false
更改为var _CalendarAccess = true
,那么在呈现VC时就是如此。此外,当VC出现时,Calendar
变量为nil
,但不是。{/ p>
答案 0 :(得分:2)
Project包含同名框架,这导致编译器在多个位置查找相同的值。删除框架,问题解决了:))