Swift:如何从位于 AppDelegate 中的 @main 调用位于 ViewController 中的方法?

发布于 2025-01-09 10:36:16 字数 903 浏览 5 评论 0原文

我是 Swift 新手,正在为入口点而苦苦挣扎。

使用版本:Swift 5.5.2 + IOS 15.3.1

我在 ViewController 中创建了一个方法,它应该在标签中显示一个单词。必须从主函数调用此方法。据我了解 main 函数必须在 AppDelegate 中 @main 之后实现。

按照我的代码。

AppDelegate:

@main struct myApp {
    static func main(){
        let newClass = ViewController()
        newClass.viewText()
    }
}

ViewController:

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var labelText: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    func viewText() {
        let word = "hello"
        labelText.text = String(word)
    }
}

通过运行应用程序,我收到以下错误消息,该消息出现在 labelText.text = String(word ):

致命错误:隐式解包可选值时意外发现 nil

非常感谢您的帮助。

I´m new to Swift and struggling with the entry point.

Used versions: Swift 5.5.2 +
IOS 15.3.1

I´ve created a method in ViewController which simply should show a word in a label. This method have to be called from a main function. According to my understanding
the main function has to be implemented in AppDelegate after @main.

Following my code.

AppDelegate:

@main struct myApp {
    static func main(){
        let newClass = ViewController()
        newClass.viewText()
    }
}

ViewController:

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var labelText: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    func viewText() {
        let word = "hello"
        labelText.text = String(word)
    }
}

By running the App I get the following error message which occurs in the line labelText.text = String(word):

Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

Your help is really appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愁以何悠 2025-01-16 10:36:16

正如您问题的评论中已经指出的那样, @main 不适用于 UI 开发,而是用于 CLI 开发(命令行界面)。

我不是 100% 确定你的意思是你有一个标签,其中有一个需要在 main 上运行的方法,所以我在这里对你的意思做出一些假设。
如果您的意思是标签应该可点击并运行操作,那么我建议您使用 UIButton 为此。

也许您在某处听说过所有 UI 都需要在 Main 上运行,这就是您的意思,但我会说在这个阶段您不需要担心这一点。默认情况下,应用程序在 main 上运行,除非您主动执行某些操作来更改此设置。请原谅我的假设,但我的假设是你对 iOS 开发相当陌生,所以我建议你先熟悉一下基础知识;例如,设置基本 UI 并按下带有操作的按钮。

我建议您从构建一个简单的 Hello,World! iOS 应用程序并从那里获取它。

As already pointed out in the comments of your question, the @main is not meant for UI development, it is for CLI development (Command Line Interface).

I am not 100% sure what you mean with that you have a label which has a method that needs to be run on main, so I'm making some assumptions here on what you mean.
If you mean that the label should be tappable and run an action, then I would recommend you use an UIButton for that.

Maybe you've heard somewhere that all UI needs to be run on Main and this is what you've meant, than I would say that at this stage you don't need to worry about that. By default the app is run on main, unless you actively do something to change this. Please forgive me in assuming this, but my assumption is that you are rather new to iOS development and so I would like to suggest that you get familiar with the basics first; e.g. setting up a basic UI and pressing a button with an action.

I would suggest you start off with building a simple Hello, World! iOS application and take it from there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文