You can do the very trick you would do if your project were Objective-C.
Create a header file and place the category declaration in the class along with the declaration of the private methods you want to open. Then tell the Swift compiler to import this bridge header.
For demonstration purposes, I will be speaking inside the NSBigMutableString
, a private subclass of NSMutableString
, and I will call it the _isMarkedAsImmutable
method.
Please note that here the whole class itself is private, so I must first declare it as a subclass of my real superclass. Because of this, I could also declare a method directly in the declaration of the class itself; which, however, would not demonstrate the use of the (Private)
category of tricks.
If your class is publicly available, then obviously you only need a category and you do not need to (re) declare the class itself.
Bridge.h:
s.swift:
let s = NSBigMutableString() println(s._isMarkedAsImmutable())
Compile and run:
$ xcrun -sdk macosx swiftc -o foo -import-objc-header Bridge.h s.swift $ ./foo false $
source share