gpt4 book ai didi

ios - Swift bool 自己改变

转载 作者:搜寻专家 更新时间:2023-11-01 07:15:44 24 4
gpt4 key购买 nike

我有一个奇怪的错误。我有一个名为 CalendarAccess 的全局 Bool:

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

但是,当我在 View Controller 中执行以下操作时,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.")
}
}

我什至不知道这怎么可能 - 该变量是一个全局变量,与任何 View 或 Controller 都没有任何关系。

最后一个代码块来自的 Controller 以模态方式呈现,如果该信息完全有用的话。

编辑 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.

编辑:当我关闭模态呈现的 View Controller (带有 saveEvent 函数的 View Controller )并记录 CalendarAccess 的值时,它是再次真实。并且未命中 setter 断点。

编辑: 当我显示 VC 时,_CalendarAccess 的值似乎回到了初始值。如果我将 var _CalendarAccess = false 更改为 var _CalendarAccess = true,则在显示 VC 时为 true。此外,当出现 VC 时,Calendar 变量为 nil,否则不会。

最佳答案

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

关于ios - Swift bool 自己改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42297741/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com