【问题标题】:I don't know where is a starting point in swift program我不知道 swift 程序的起点在哪里
【发布时间】:2015-05-06 10:53:13
【问题描述】:

我是 swift 的初学者。我知道 Swift 是按顺序处理的,但是程序有一个起点,我认为它不是 xcodeproj 中的 Viewcontroller.swift。 起点在哪里?

【问题讨论】:

标签: swift


【解决方案1】:

所有应用程序,无论是 Swift 还是 Objetive C,都以 main 类开头。但是 swift 自动实现了由 @UIApplicationMain 属性生成的 main 。 @UIApplicationMain 的简单实现如下:

UIApplicationMain(C_ARGC, C_ARGV, nil, NSStringFromClass(AppDelegate))

表示运行AppDelegate 类。

让我们从AppDelegate 中注释掉@UIApplicationMain 并运行。你会因为两个错误而构建失败,比如

Undefined symbols for architecture x86_64: "_main", referenced from: implicit entry/start for main executable ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation).

看,它确实需要一个 main 作为入口/起点。

现在创建一个 main.swift 文件并编写以下代码并再次运行。

import UIKit
UIApplicationMain(C_ARGC, C_ARGV, nil, NSStringFromClass(AppDelegate))

它将成功编译并加载与@UIApplicationMain完全相同的视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-09
    • 1970-01-01
    • 2023-03-30
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    • 1970-01-01
    相关资源
    最近更新 更多