Swift and prototype method - forward declaration

Learning about Swift headers I see this pattern used by Apple, specifically the init declaration of the following HAS NO IMPLEMENTATION structure. Obviously, the implementation of init () is hidden somehow since it is Apple, but I was trying to figure out how to do it. This is just an example, but it looks like the general behavior in the headers

struct AutoreleasingUnsafePointer<T> : Equatable, LogicValue { let value: Builtin.RawPointer init(_ value: Builtin.RawPointer) // <---- how is it possible? where is the implementation? func getLogicValue() -> Bool /// Access the underlying raw memory, getting and /// setting values. var memory: T } 

I know that you can declare a protocol plus an extension of the class, doing this to β€œhide” the implementation from the class declaration and move it to another place

 class TestClass :TestClassProtocol { // nothing here } protocol TestClassProtocol { func someMethod() // here is the method declaration } extension TestClass { func someMethod() // here is the method implementation { println("extended method") } } 

But this is different from what I saw in the Apple headers, as the "declaration" method is inside the "class" and not inside the "protocol". however, if I try to place a method declaration inside the TestClass class, I have two errors (a function without a body in the class and an invalid re-declaration on the extension)

In Objective-C, this was "implicit" since the method declaration was in .h and implementation in .m. How to do the same in Swift?

+7
ios swift
source share
1 answer

I think the explanation is very simple. What you see in Xcode is not really valid Swift code.

This is the result of automatically converting the Obj-C header to Swift code, but it does not compile Swift.

+2
source share

All Articles