Search code examples
iosswiftcocoa-touchuiviewcontrollereventkit

Swift bool changes by itself


I have this weird bug. I have a global Bool called CalendarAccess:

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

As you can see, I made a setter and getter for it so I could put a breakpoint to see where it is being set. I did that and every single time I hit the breakpoint the value is true. I'm sure I never set the _CalendarAccess directly because this variable was called just CalendarAccess until just now.

But, when I do the following in a view controller, CalendarAccess is 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.")
    }
}

I have no idea how this even is possible - the variable is a global that doesn't have anything to do with any view or controller.

The controller the last code block is from is presented modally, if that information is useful at all.

EDIT: CalendarAccess is set in only one place (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
}

EDIT: I get these messages when I tap the button that calls the @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.

EDIT: When I dismiss the modally presented view controller (the one with the saveEvent func) and log the value of CalendarAccess, it's true again. And the setter breakpoint isn't hit.

EDIT: It seems like the value of _CalendarAccess goes back to the initial value when I present the VC. If I change var _CalendarAccess = false to var _CalendarAccess = true it is true when the VC is presented. Also, the Calendar variable is nil when the VC is presented, but not otherwise.


Solution

  • Project had same-named framework included, which was causing the compiler to look in multiple places for the same values. Remove framework, problem solved :)