Here is the code for my main.swift file - as the code shows, I still use a "MainMenu" nib that loads the standard menu items in Interface Builder:
import Cocoa
private func setDelegate(
application: NSApplication
, delegate: NSApplicationDelegate? )
-> NSApplication {
application.delegate = delegate
return application
}
private func runApplication(
application: NSApplication = NSApplication.sharedApplication()
, delegate: NSApplicationDelegate? = nil
, bundle: NSBundle = NSBundle.mainBundle()
, nibName: String = "MainMenu"
, var topLevelObjects: NSArray? = nil ) {
if bundle.loadNibNamed(
nibName
, owner: setDelegate(application, delegate: delegate)
, topLevelObjects: &topLevelObjects ) {
application.run()
} else {
print("An error was encountered while starting the application.")
}
}
runApplication(delegate: AppDelegate())
And here is my AppDelegate file:
import Cocoa
@NSApplicationMain // This is commented out when I am using main.swift;
// That is the only difference in my AppDelegate file between the two scenarios
class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
let mainWindow = MainWindow()
func applicationDidFinishLaunching(notification: NSNotification) {
mainWindow.makeKeyAndOrderFront(self)
}
}
When I instantiate AppDelegate in Interface Builder I don't use a storyboard. I am using a nib. It is just one of the Blue Cube Objects from the object selector menu, dragged over to the object pane on the lefthand side of IB, with the delegate outlet connected to the File's Owner icon.
In either case, my info.plist file still specifies "MainMenu" as being the nib name that gets loaded when the application starts, since I use the "MainMenu" nib to configure the menus in either scenario.
So in scenario one:
1: I use main.swift and comment out @NSApplicationMain from my AppDelegate file
2: I delete the AppDelegate icon from the lefthand side of IB, so that the only nib icon that remains is the "Main Menu" icon (the File's Owner and First Responder icons are also still there.)
And in scenario two:
1: I remove the main.swift file from the application and uncomment @NSApplicationMain in the AppDelegate file
2: I leave the AppDelegate icon in Interface Builder and it is connected to the delegate outlet of the File's Owner icon.
Other than that, everything is exactly the same as far as I can tell.
Thanks for taking the time to respond, by the way...
[EDIT] Just for the sake of completeness, here is the code for my MainWindow and TableView classes - other than these two classes and the code above, there is no other code in the entire application:
import Cocoa
final class MainWindow: NSWindow, NSWindowDelegate {
init() {
let styleMask =
NSResizableWindowMask
| NSMiniaturizableWindowMask
| NSClosableWindowMask
| NSTitledWindowMask
let backingStore = NSBackingStoreType.Buffered
super.init(contentRect: NSMakeRect(320, 200, 640, 400), styleMask: styleMask, backing: backingStore, `defer`: true)
delegate = self
let view = NSView(frame: contentRectForFrameRect(frame))
contentView = view
let scrollView = NSScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
view.subviews = [ scrollView ]
let viewDict =
[ "scrollView": scrollView ]
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
"H:|[scrollView(640)]|"
, options: .AlignAllTop
, metrics: nil
, views: viewDict ) )
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
"V:|[scrollView(400)]|"
, options: .AlignAllLeading
, metrics: nil
, views: viewDict ) )
let tableView = TableView()
scrollView.documentView = tableView
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
}
final class TableView: NSTableView, NSTableViewDelegate, NSTableViewDataSource {
let data: [String] = {
return (0..<25).map { "Item No. \($0)" }
}()
init() {
super.init(frame: NSZeroRect)
enabled = true
let tableColumn = NSTableColumn(identifier: "mainColumn")
tableColumn.headerCell.title = "Main Column"
addTableColumn(tableColumn)
setDelegate(self)
setDataSource(self)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
func numberOfRowsInTableView(tableView: NSTableView) -> Int {
return data.count
}
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {
let view = NSTextField()
view.editable = true
view.selectable = true
view.stringValue = data[row]
view.drawsBackground = false
view.bordered = false
return view
}
}