Start with the app delegate.
, and mark the class with
, because it is now unnecessary. With the use of
, the target now knows the entry point.
When you add your first Swift file (like
above), Xcode will prompt to add a bridging header.
Add the bridging header.
is the place where you add your existing .h files, exposing them to Swift (as and when needed).
We come to the first of the 2 important parts in interoperability .
To use your existing Objective-C, there are a few things to do:
We have mentioned (1) in the section above.
For (2), the reason is because Swift is strict on nullability . Can an object be optional, or never?
In Objective-C days, way before Swfit was born, nullability was never explicit. You would have seen
, and many times the reason is because the object is nil, when is should not be.
Swift make our code safer with explicit nullability.
Hence, you need to make Objective-C code be explicit, so that Swift can use safely:
nullable- An optional
nonnull- Never an optional
There are also 2 convenient macros to help you:
. Any properties between the macros are considered
This is the second part, if you still need to write more Objective-C.
To use the new Swift code in existing Objective-C, you have to
This will expose all Swift code.
Don’t be alarm that you cannot find the aforementioned .h file in your project. It is a auto generated header from your Swift code.