Lan*_*ard 7 macos cocoa command-line compilation swift
我有这 3 个文件:
索引.swift
import Cocoa
print("Init")
let app = NSApplication.shared()
let delegate = AppDelegate() // alloc main app's delegate class
app.delegate = delegate // set as app's delegate
app.run()
print("End")
Run Code Online (Sandbox Code Playgroud)
src/delegate.swift
import Cocoa
class AppDelegate: NSObject, NSApplicationDelegate {
var newWindow: NSWindow?
var controller: ViewController?
override init() {
print("App")
super.init()
mainView()
}
func mainView() {
print("View")
newWindow = NSWindow(contentRect: NSMakeRect(10, 10, 200, 200), styleMask: [.titled, .resizable, .closable], backing: .buffered, defer: false)
let content = newWindow!.contentView! as NSView
controller = ViewController()
//controller?.loadView()
let view = controller!.view
content.addSubview(view)
newWindow!.makeKeyAndOrderFront(nil)
}
func applicationDidFinishLaunching(aNotification: NSNotification) {
print("Launch") // Not showing this?
}
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
print("Bye")
return true
}
}
Run Code Online (Sandbox Code Playgroud)
src/controller.swift
import Cocoa
class ViewController : NSViewController {
var name = "Main"
var button: NSButton?
@IBAction func onClick(_: Any) {
print("Click")
}
override func loadView() {
print("Load")
let view = NSView(frame: NSMakeRect(0,0,200,200))
view.wantsLayer = true
view.layer?.borderWidth = 2
view.layer?.borderColor = NSColor.red.cgColor
button = NSButton(frame: NSMakeRect(20, 20, 100, 32))
button?.title = "Click Me"
button?.target = self
button?.action = #selector(onClick)
view.addSubview(button!)
self.view = view
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试像这样编译它:
swift index.swift src/delegate.swift src/controller.swift
Run Code Online (Sandbox Code Playgroud)
但出现此错误:
index.swift:7:16: error: use of unresolved identifier 'AppDelegate'
let delegate = AppDelegate() // alloc main app's delegate class
^~~~~~~~~~~
Foundation.PortDelegate:1:17: note: did you mean 'PortDelegate'?
public protocol PortDelegate : NSObjectProtocol {
^
CoreText.CTRunDelegate:1:14: note: did you mean 'CTRunDelegate'?
public class CTRunDelegate {
^
<unknown>:0: warning: 'cacheParamsComputed' is deprecated
<unknown>:0: warning: 'cacheAlphaComputed' is deprecated
<unknown>:0: warning: 'keepCacheWindow' is deprecated
<unknown>:0: error: 'memoryless' is unavailable
Metal.MTLCommandBufferError:55:14: note: 'memoryless' has been explicitly marked unavailable here
case memoryless
^
Run Code Online (Sandbox Code Playgroud)
想知道如何进行此编译。基本上,我只需要知道如何从当前定义到 index.swift 范围的位置要求/导入这些类 AppDelegate 和 ViewController 。如果我将所有代码放入 1 个文件 index.swift,那么它可以正常编译。但我想像在 XCode 中那样模块化它,但不调用 XCode。想知道是否可以通过某种 plist 来解决这个问题。我尝试swift -F
指定swift -I
导入目录,但没有成功。
正如@MartinR所说,swiftc
是编译器。您的swift
命令调用解释器,而不是编译器。我认为解释器只能运行单个文件。
我只浏览了以下几页,但它们可能会帮助您运行由多个文件组成的 Swift 程序。
swift
:swift package init --type executable
swift build
将项目编译为单个可执行文件我通过搜索找到了这些(以及许多类似的)页面:swift command line run multiple files
归档时间: |
|
查看次数: |
13595 次 |
最近记录: |