Xcode中的单个.swift文件是否可以包含iOS应用程序的完整源代码?

时间:2023-01-23 18:01:58

In other words, can Swift code in Xcode behave like it does in a Playground file?

换句话说,Xcode中的Swift代码可以像在Playground文件中那样运行吗?

3 个解决方案

#1


7  

It certainly can.

当然可以。

This special file is called main.swift and it behaves just like a Playground file.

这个特殊的文件名为main.swift,它的行为就像一个Playground文件。

From Files and Initialization, a post on Apple Swift Blog:

来自文件和初始化,Apple Swift博客上的帖子:

… The “main.swift” file can contain top-level code, and the order-dependent rules apply as well. In effect, the first line of code to run in “main.swift” is implicitly defined as the main entrypoint for the program. This allows the minimal Swift program to be a single line — as long as that line is in “main.swift”.

...“main.swift”文件可以包含*代码,并且依赖于顺序的规则也适用。实际上,在“main.swift”中运行的第一行代码被隐式定义为程序的主要入口点。这允许最小的Swift程序是单行 - 只要该行在“main.swift”中。

However, on iOS, @UIApplicationMain is the application entry point. It does the whole startup work for you automatically. But in main.swift of an iOS app, you have to initialize it manually.

但是,在iOS上,@ UapppplicationMain是应用程序入口点。它会自动为您完成整个启动工作。但是在iOS应用程序的main.swift中,您必须手动初始化它。

In Swift 3.1, your main.swift file should begin like this:

在Swift 3.1中,你的main.swift文件应该像这样开始:

// main.swift

import Foundation
import UIKit

class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
}

UIApplicationMain(
    CommandLine.argc,
    UnsafeMutableRawPointer(CommandLine.unsafeArgv)
        .bindMemory(
            to: UnsafeMutablePointer<Int8>.self,
            capacity: Int(CommandLine.argc)),
    nil,
    NSStringFromClass(AppDelegate.self)
)

// Your code begins here

UIApplicationMain initialization provided by Matt Neuburg.

UIApplicationMain初始化由Matt Neuburg提供。

#2


2  

Yes, you could do this just as you could write all your code on one line of code if you really wanted to with semicolons to end your lines. Although, this is definitely not recommended as it can cause lots of confusion and you have to bother messing with the original organized files.

是的,您可以这样做,就像您可以在一行代码上编写所有代码一样,如果您真的想用分号来结束行。虽然,这绝对不推荐,因为它可能会导致很多混乱,你不得不打扰原始有组织的文件。

#3


1  

Yes it does, except the AppDelegate though. It is not recommended to do that, but in one file you can have multiple classes.

是的,但除了AppDelegate之外。不建议这样做,但在一个文件中,您可以有多个类。

#1


7  

It certainly can.

当然可以。

This special file is called main.swift and it behaves just like a Playground file.

这个特殊的文件名为main.swift,它的行为就像一个Playground文件。

From Files and Initialization, a post on Apple Swift Blog:

来自文件和初始化,Apple Swift博客上的帖子:

… The “main.swift” file can contain top-level code, and the order-dependent rules apply as well. In effect, the first line of code to run in “main.swift” is implicitly defined as the main entrypoint for the program. This allows the minimal Swift program to be a single line — as long as that line is in “main.swift”.

...“main.swift”文件可以包含*代码,并且依赖于顺序的规则也适用。实际上,在“main.swift”中运行的第一行代码被隐式定义为程序的主要入口点。这允许最小的Swift程序是单行 - 只要该行在“main.swift”中。

However, on iOS, @UIApplicationMain is the application entry point. It does the whole startup work for you automatically. But in main.swift of an iOS app, you have to initialize it manually.

但是,在iOS上,@ UapppplicationMain是应用程序入口点。它会自动为您完成整个启动工作。但是在iOS应用程序的main.swift中,您必须手动初始化它。

In Swift 3.1, your main.swift file should begin like this:

在Swift 3.1中,你的main.swift文件应该像这样开始:

// main.swift

import Foundation
import UIKit

class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
}

UIApplicationMain(
    CommandLine.argc,
    UnsafeMutableRawPointer(CommandLine.unsafeArgv)
        .bindMemory(
            to: UnsafeMutablePointer<Int8>.self,
            capacity: Int(CommandLine.argc)),
    nil,
    NSStringFromClass(AppDelegate.self)
)

// Your code begins here

UIApplicationMain initialization provided by Matt Neuburg.

UIApplicationMain初始化由Matt Neuburg提供。

#2


2  

Yes, you could do this just as you could write all your code on one line of code if you really wanted to with semicolons to end your lines. Although, this is definitely not recommended as it can cause lots of confusion and you have to bother messing with the original organized files.

是的,您可以这样做,就像您可以在一行代码上编写所有代码一样,如果您真的想用分号来结束行。虽然,这绝对不推荐,因为它可能会导致很多混乱,你不得不打扰原始有组织的文件。

#3


1  

Yes it does, except the AppDelegate though. It is not recommended to do that, but in one file you can have multiple classes.

是的,但除了AppDelegate之外。不建议这样做,但在一个文件中,您可以有多个类。