gpt4 book ai didi

ios - 为什么模拟器中的 UUID 为 nil?

转载 作者:行者123 更新时间:2023-11-28 10:55:36 38 4
gpt4 key购买 nike

我正在完全使用 Swift 开发 tvOS 应用。

AppDelegate.swift 中,我尝试在 willFinishLaunchingWithOptions 下打印 UUID

但是,错误消息显示 fatal error :在展开可选值时意外发现 nil

该应用程序实际上可以正常运行并且之前运行良好。这个错误发生在我重置模拟器之后。

这是我获取 UUID 的方法。

let UUIDValue = UIDevice.current.identifierForVendor!.uuidString
let newuuid = UserDefaults.standard.object(forKey: "uuid") as? String
print ("This device uuid is " + newuuid!) // error message happens here

我相信每个设备都应该包含一个UUID,但这是怎么发生的呢?

最佳答案

您正在尝试从 UserDefaults 获取 newuuid:

let newuuid = UserDefaults.standard.object(forKey: "uuid") as? String

但在重置模拟器后,UserDefaults 中的条目为零,并且尚未为键“uuid”设置值。

这没有多大意义,但解决了问题:

let uuidValue = UIDevice.current.identifierForVendor!.uuidString
UserDefaults.standard.setValue(uuidValue, forKey: "uuid") // add entry in UserDefaults
let newuuid = UserDefaults.standard.object(forKey: "uuid") as? String
print ("This device uuid is " + newuuid!)

关于ios - 为什么模拟器中的 UUID 为 nil?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43897475/

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