For developers who are already working, it is not appropriate or conditional to start with a new Swift project. While the online tutorials on swift and OC mixed programming are much more, but mostly based on the SWIFT project, there is a certain discount on the reference value of our ocer.
There are still a lot of flaws in swift, but given the strength of Apple, it is time to give Swift a simple function in the project, giving itself, to the project, to swift a buffer, which is excellent.
Say so much, start on the code.
Now the background is: there is already a OC project, I want to join Swfit, and implement OC Invoke Swift, Swift calls OC.
1. Create a new Swift file
2. Next, you will be asked whether to set up a bridging file, click Yes, then I was redundant, so choose to cancel, we manually set up the bridge file
3. Create a bridging file manually: Create a new header file
About naming: Experienced people know that this header file is common to the project, and if created automatically, the file name is:
Project name-bridging-header.h
But because my current project name is Chinese, I named it myself.
4. Configure the header file
search for Swift in build setting and configure it according to the actual situation:
Note: Is relative to the relative path where the project file is located. Look inside the Finder, don't let the group in the project Disturb your judgment.
5. Introduce the required OC header file in the header file, I'll use the Uikit.
6. Writing code
As you can see, we can use the Uikit class.
Note: In the case of custom classes, you want a good naming convention, you will benefit from mixed programming.
7.oc Call Swift
Now because of the test I want to set the window's root controller to the one of my swift.
Because Swift does not have a header file, so it is difficult to do ...
In fact, the system defaults to the generation of the header file (our Swift class is the direct or indirect subclass of the NSObject), we just need to introduce
Project Name-swift.h
No way, my project name is Chinese ...
Introduced:
8. Classes that use Swift
9. Running
Everything is ok (ugly, don't throw up the groove)
Adding swift files to the OC Project and implementing mixed programming