Swift Bool自行改变



我有这个怪异的错误。我有一个名为CalendarAccess的全局Bool

var EventStore: EKEventStore!
var Calendar: NSCalendar!
var CalendarAccessRequestComplete = false
var _CalendarAccess = false
var CalendarAccess: Bool {
    set(value)
    {
        _CalendarAccess = value;
    }
    get {
        return _CalendarAccess;
    }
}

您可以看到,我为它做了一个设定器和Getter,因此我可以放假点以查看设置的位置。我这样做了,每当我击中断点时,值都是true。我敢肯定我从来没有直接设置_CalendarAccess,因为此变量直到现在只称为CalendarAccess

但是,当我在视图控制器中进行以下操作时, CalendarAccessfalse

@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的值时,又是真的。而且设置器断点未击中。

编辑:当我介绍VC时,_CalendarAccess的值似乎回到了初始值。如果我将var _CalendarAccess = false更改为var _CalendarAccess = true,则当出现VC时是正确的。另外,当出现VC时,Calendar变量是nil,但不是其他。

项目包含相同的框架,这导致编译器在多个位置查看相同值。删除框架,解决问题:)

相关内容

  • 没有找到相关文章

最新更新