Swift was released at the WWDC conference. Finally, a lot of modern programming languages are added. In the early years, when I was playing C #3.0 +, these were similar.
The well-known things are finally wakened when you read swift Programming Language 1.1.
Compared with objective-C, SWIFT is almost a new language. Fortunately, swift and OC come from the apple family and can eat at a table. That is to say, in the Upgrade Project
You can consider using SWIFT to develop new functions to call existing or existing calls. You can also use the existing OC and C libraries in swift development projects. This way
Swift and OC share the same process of developing an app or framework. Of course, mixed language development apps and frameworks are slightly different.
Let's talk about mixed code programming during app development.
You cannot directly introduce the added OC code in the SWIFT code app. So when you add the OC file to the swift app or the swift file to the OC app, xcode will
Ask if you want to create a bridging header. Files with this bridging headerswfit and OC can access each other.
The generated special header file is named by adding "-Bridging-Header.h" to your app name. For example, if the name of my app is myswiftapp
What is generated is called "MySwiftApp-Bridging-Header.h ".
Introduce OC code in swift
1. Introduce the header file you want to expose to SWIFT in the briding header file of OC. For example:
1 #import "XYZCustomCell.h"2 #import "XYZCustomView.h"3 #import "XYZCustomViewController.h"
2. Make sure that the briding header path in build settings points directly to the file itself, rather than its directory. Like this:
In general, this file is generated by default, so you don't have to worry about it.
As long as the OC header file is placed in the briding header, it is visible in swift. There are no import statements. Methods in the OC file are also visible in swift, and arbitrary swift files are visible. Then you can call your OC code like the SWIFT code generated by the system.
1 let myCell = XYZCustomCell()2 myCell.subtitle = "A custom cell"
Introduce in oCSwiftCode
This is much more convenient. When you cannot see it, the system generates a header file named "-Swift. H" by default. Obviously, this is an oc-style header file, because you need to call Swift code in the OC code. This header file is the same as the briding header file. Note that the system will generate this file by default if you do not need to do anything. You need to know the naming method of this file: "Your app name-Swift. H ". Import this file in the OC source file that needs to call Swift code. All OC types in this file are available. If you use a custom OC type in swift, remember to import your custom OC type file before the automatically generated File Import! Example:
# Import "your app name-Swift. H"
After this file is included, all SWIFT files are visible in the source file of this oC.
Here is a summary. To use OC in swift, you need an explicitly generated briding header file. All the OC files listed in this file are visible in swift. That is, there is no need to go anywhere.Import the briding header file. When swift is used in OC, xcode will implicitly generate a "your app name-Swift. H" file by default, where to use it to import this header in the OC source file there. All types of swift files are visible to this source file. If other custom OC types are used in swift, introduce the header file of the custom OC type before "your app name-Swift. H" is imported.
Use Code together in the same framework
Use OC in swift.There seems to be an error in this original article. For details, see:
1. First, make sure that the allow non-modular modules in framework modules of build settings is set to Yes (Note: In the original article, defines module setting for that framework target is set to yes. But here the default value is yes, and an error will be reported after the code is added ). Or put all the OC code you added into the public of build targe (project by default ).
2. In the umbrella header file, import the header file that you want to expose to Swift's OC code. For example:
# Import <myswiftframework/occode. h>
Note: You need to add the framework name before the header file of Your OC!
In this way, Swift can access all the OC header files introduced in the umbrella header file. At the same time, the source file content corresponding to these OC header files can be automatically accessed in Swift code without other imports. Use the introduced OC type according to the swift syntax, such:
1 import Foundation2 3 class SwiftClass{4 init(){5 6 }7 8 var test_code = OCCode()9 }
Introduce swift in oC
Introduce Swift code in the code base of OC code. You do not need to introduce any swift files in the umbrella header file.
This part cannot be tested according to the document and will be updated later.