Don't do this; code responsibly
Please don't do this. It's horrible, unsupported, and may crash your program.
That said, let's demangle a class name by calling an internal Swift library function.
@asmname("swift_demangleSimpleClass") func demangleSimpleClass(mangledName: ConstUnsafePointer<Int8>, moduleName: UnsafePointer<Int>, className: . . .
There's an awful joke in here somewhere
David Owens II writes about Swift access control and unit tests. He's currently adding his source files directly to the unit test target so the internal members are visible to the tests.
That's probably the best solution possible currently, but I'm filing a radar to add the equivalent of
InternalsVisibleTo. In C#, that attribute . . .
Who wants to live without extensions
Optionals are great, but there are some improvements that might make code clearer and more explicit. I also want to acknowledge that in some cases you don't actually care if the value is nil or not, you just want to provide an alternate value if it is; that's usually called the null coalescing operator, but Swift doesn't currently . . .
Abandon all hope of safety
Update: I originally did not make it clear but if Swift knows the type of the unsafe pointer then arithmetic will operate on
sizeof(T) not on bytes! The same goes for
alloc. So my example below with an
UnsafePointer<Float32> will jump forward
25 * sizeof(Float32) bytes.
I've seen a lot of people wondering how to do raw pointer . . .
Prayers answered, denied
Here are some of the more notable Swift changes as of Beta 4:
The new access control keywords are available:
|private||can only be accessed from the source file where defined|
|internal||can only be accessed from the current target where defined|
|public||can be accessed anywhere, including any context that imports . . .|
Interop in the real world
So far we've been going over some of the interop capabilities in Swift, but today I want to switch gears a bit and actually put this stuff to use. Along the way we'll discover some annoyances and take opportunities not available in Objective-C to remedy them.
Let's say we want to determine if a host is reachable. To do that, we . . .
The easy way
Today, let's look at Swift's implicit pointer support which can simplify our code quite a bit and works great when the callee doesn't keep the pointer alive or when it understands Objective-C and can handle retain/release/autorelease semantics. For some of the readers this will be extremely simple stuff, but I want to cover it for . . .