last day (14 days later) » 

2:13 PM
0
Q: Swift bool changes by itself

edvinHolmI 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 { ...

 
So I added a print(_CalendarAccess) below the _CalendarAccess = true line. That prints true one time before the the saveEvent function is called. But still, I get the error alert.
@Magnas, I do set the CalendarAccess.
The variable is set in other places in the code base. It is always set to true, which is proven by the print, but in the saveEvent function I get false.
@MartinR, it's hard to do because I'd probably not get the same result then.
I put a breakpoint in the setter, and the only time the variable is set is in the application:didFinishLaunchingWithOptions: (See edit)
I did that, and it prints false. I am sure saveEvent is called after application:didFinishLaunchingWithOptions: since it is an @IBAction called by a button, and when I put a breakpoint in the setter I hit that before I tap the button.
Also searched the workspace for "CalendarAccess". It is just get everywhere except in application:didFinishLaunchingWithOptions:.
@simonWasHere That has nothing to do with my question.
@simonWasHere What is so bad about it?
 
Search your project for "var CalendarAccess" and "let CalendarAccess" .. see if you inadvertently created another instance (shouldn't be able to, but worth a quick text search).
 
Yeah, I've already done that.
 
"... EDIT: When I dismiss the modally presented view controller with the saveEvent func and log the value of CalendarAccess, it's true again. And the setter breakpoint isn't hit. ..." When reach the line if(CalendarAccess) in saveEvent() in your modally presented VC, is the getter breakpoint hit?
 
Yes, the getter breakpoint is hit.
 
2:13 PM
OK, so do you have anywhere in your code where you evaluate CalendarAccess and it is true? And there is only one occurrence of "CalendarAccess =" in all of your project?
 
Yes, as I've said many times, it is true as long as the modal VC isn't presented. I added a button in both VCs to tap on to log the value of the variable. And I have searched the workspace for CalendarAccess and there is only one place where I set it.
 
Hmmm... SO wants to move this to chat, but gave an error when trying to do so... meh. At this point, I'd say it's unlikely anybody will come up with an answer without seeing more of the code - or at least a small project that reproduces the issue. For an interim solution, you could pass the current value of CalendarAccess into the VC when you present it. If it were me, I think I'd distill it down to a "here, run it and see for yourself" example.
 
I tried that but were unable to reproduce the behavior. I could give you the whole project, it isn't big at all... Don't know how though.
 

last day (14 days later) »