Xcode 6 is still pre-release and the tools used for combining ObjectiveC/C++ with Swift may change and are currently difficult to use. I’ll hold off committing most changes until Xcode 6 is released. The use of NS_ENUM inside InfoBarCommunicator.h looks to be a good change anyway so will be committed soon.
There are a couple of mechanisms that can be used to expose the Objective C/C++ definitions to Swift. A bridging header was first tried but this had some problems. Then a module map was used and this worked well although I don’t really understand modules. The current module.modulemap for Scintilla is:
framework module Scintilla {
umbrella header "ScintillaView.h"
export *
module * { export * }
}
This works since ScintillaView.h includes the Scintilla.h, SciLexer.h, and InfoBarCommunicator.h so exports everything that might be wanted by client code. It also does not require a “Scintilla.” prefix before every feature as other variants did but I don’t understand why. Here is the main file AppDelegate.swift for a working Swift application that looks like
http://scintilla.org/Swiftee.png
// AppDelegate.swift in Swiftee
// Copyright 2014 Neil Hodgson.
// This code is public domain.
import Cocoa
import Scintilla
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet var window: NSWindow
var sci: ScintillaView?
// sciMsg is just to avoid a lot of casts since Swift is picky and
// a CInt can't be passed as a CLong without a cast.
func sciMsg(m:CInt, wp:CInt=0, lp:CInt=0) {
sci?.message(CUnsignedInt(m),wParam: CUnsignedLong(wp),lParam:CLong(lp))
}
func applicationDidFinishLaunching(aNotification: NSNotification?) {
sci = ScintillaView(frame: window.contentView.bounds)
if let ssci = sci {
ssci.autoresizingMask = NSAutoresizingMaskOptions.ViewWidthSizable |
NSAutoresizingMaskOptions.ViewHeightSizable
}
window.contentView.addSubview(sci)
sciMsg(SCI_STYLECLEARALL)
sciMsg(SCI_SETLEXER, wp:SCLEX_CPP)
sciMsg(SCI_STYLESETBOLD, wp:SCE_C_OPERATOR, lp:1)
sciMsg(SCI_STYLESETFORE, wp:SCE_C_COMMENTLINE, lp:0x008000)
sciMsg(SCI_STYLESETFORE, wp:SCE_C_WORD, lp:0x800000)
sciMsg(SCI_STYLESETFORE, wp:SCE_C_WORD2, lp:0xA00060)
sciMsg(SCI_STYLESETFORE, wp:SCE_C_STRING, lp:0x800080)
sciMsg(SCI_STYLESETITALIC, wp:SCE_C_STRING, lp:1)
sci?.setStringProperty(SCI_SETKEYWORDS, parameter: 0,
value: "class func import let return var @IBOutlet")
sci?.setStringProperty(SCI_SETKEYWORDS, parameter: 1,
value: "CInt CLong CUnsignedInt CUnsignedLong")
sci?.insertText("// Multiply by 5\nfunc mult5(a: CInt) -> CInt {\n\treturn a * 5;\n}\n")
}
func applicationWillTerminate(aNotification: NSNotification?) {
// Insert code here to tear down your application
}
}
The module map shown above should also work for Objective C client code but I was unable to make it build. Modules fix the use of textual inclusion in C / Objective C / C++ as they allow the compiler to parse each header once to produce a module then reuse the parsed form whenever the module is imported. This should speed up compilation as each header line is often parsed many times: for Scintilla+SciTE on Windows, according to Header Hero, the compiler parses 37 million header lines so each line is parsed around 91 times in a build.
http://clang.llvm.org/docs/Modules.html
Neil