Can a single .swift file in Xcode contain all the source code of an iOS application?

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

+6
source share
3 answers

It certainly can.

This special file is called , and it behaves exactly the same as the file in the playground. main.swift

From Files and Initialization , Apple Swift Blog Post:

... main.swift , , . , "main.swift" . Swift line - , "main.swift".

, iOS, @UIApplicationMain - . . main.swift iOS .

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 ​​Matt Neuburg.

+7

, , , . , , , , .

+2

Yes, yes, except for AppDelegate. This is not recommended, but you can have several classes in one file.

+1
source

Source: https://habr.com/ru/post/1016478/


All Articles