gpt4 book ai didi

ios - 在展开可选值时意外发现 nil,当尝试更改标签的文本时,由通知触发

转载 作者:行者123 更新时间:2023-11-28 06:13:43 25 4
gpt4 key购买 nike

目前,我的 AppDelegate.swift 文件中有一个函数,它会在收到通知时触发。我正在使用 Firebase 发送这些通知。

当该函数运行时,它会使用键“url”检查是否有任何额外的附加数据,如果是,它会在我的 ViewController.swift 文件中运行一个函数。

问题是在 ViewController 中运行的函数试图将标签的文本更改为“函数已运行”,但是当运行此行时,它会抛出错误“在展开可选时意外发现 nil值”。

我不知道这个 Optional 是什么,所以如果有人能提出我的代码可能存在的问题,那就太好了。

我尝试过的事情:

  • 创建和使用新标签
  • 重命名标签
  • 检查 socket 是否正确连接
  • 检查类设置是否正确

AppDelegate.swift 代码片段:

import UIKit
import Firebase
import FirebaseInstanceID
import FirebaseMessaging
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var vc = ViewController()

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

print(userInfo)

let url = userInfo["url"]

print("url is: \(String(describing: url))")

if url != nil {

hasRun = true

vc.changeLabel()

print("Func done")

}

completionHandler(UIBackgroundFetchResult.newData)
}
}

ViewController.swift 代码片段:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var theLabel: UILabel!

func changeLabel() {

theLabel.text = "The change label code has run here"

}
}

最佳答案

可选部分是您的标签属性。它被标记为带有“!”的强制解包最后@IBOutlet weak var theLabel: UILabel! .这是连接 socket 时的默认设置,为了避免此类问题,我建议您练习 @IBOutlet private weak var <#name#>: <#type#>? .

正如@Larme 在评论中已经提到的,您正在通过空构造函数创建 View Controller 的实例,默认情况下它不会从 Storyboard或 xib 加载 Controller (尽管您可能已经覆盖了它)。即使覆盖了 View ,此时可能还没有加载,您的标签仍然是 nil .

从您发布的代码来看,您似乎想要将一些数据推送到 View Controller ,除非您的应用程序通过触发通知打开,否则该 View Controller 应该存在。有很多方法可以解决这个问题,但您可能会考虑使用静态方法 changeLabel可以在不创建 View Controller 实例的情况下调用它。该方法应检查是否加载了 View Controller ,如果加载了则调用您的方法。如果尚未加载,则应在加载后在 View did load 中调用它(假设它在默认 UI 管道期间加载)。

要实现这样的系统,您可以执行以下操作:

在你的 View Controller 中添加一个私有(private)的静态变量,比如 private var currentInstance: ViewController? .在 View 中加载调用 ViewController.currentInstance = self .还为您需要的数据添加静态变量(如您的网址)static var myURL: Any? .然后:

static func setURL(url: Any?) {
if currentInstance != nil {
currentInstance.changeLabel() // Will be called immediately
} else {
myURL = url
}
}

并且 View 确实加载了:

override func viewDidLoad(animated: Bool) {
...
if let url = ViewController.myUrl {
ViewController.myUrl = nil
changeLabel()
}
...
}

通过这种方式,您在应用委托(delegate)中所需要做的就是调用 ViewController.setURL(userInfo["url"]) .

我希望你有一个基本的想法......

关于ios - 在展开可选值时意外发现 nil,当尝试更改标签的文本时,由通知触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45734715/

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