After the swift language comes out, new projects may be developed directly using SWIFT, but some situations may occur in the process, some classes or encapsulated modules that have been written with OC, if you don't want to write it again in swift, you can use mixed encoding. This is allowed in ios8.
First, it is simple to start with, first study the mixed use in the same project directory.
For demonstration. Prepare two classes first
The first class is written in the swift language. The file name is act. Swift.
import Foundationclass Act : NSObject{ func hasAct(tag:Int) -> String { switch (tag) { case 1:return "Movie" case 2:return "CCTV" case 3:return "Sport TV" default:return "Area TV" } } init() { println("act constructor is called.") } deinit { println("act destroyed is called.") }}
The second is the Class header file written in OC is occhannel. h, and the implementation file is occhannel. M.
Header file
#import <Foundation/Foundation.h>@interface OCChannel : NSObject@property (nonatomic,retain) NSString *ChannelName;- (NSString *)ChannelChange:(NSInteger) channels;@end
Implementation File
# Import "occhannel. H "# import" SwiftModule-swift.h "@ interface occhannel () {act * Act; // swift class} @ end @ implementation occhannel-(ID) init {If (Self = [Super init]) {nslog (@ "oc constructor is called. "); // use the swift class act = [[Act alloc] init];} return self;}-(void) dealloc {nslog (@" oc destroyed is called. "); // [Act release]; // arc not use // [Super dealloc]; // arc not use}-(nsstring *) channelchange :( nsinteger) channels {return [Act hasact: Channels] ;}@ end
This occhannel references the act class written by SWIFT. The main purpose is to demonstrate that the swift class calls OC in the same project, and the OC class also calls Swift. To form a mixed writing mode.
The procedure is as follows:
1. Create a SWIFT project: here the project name is mixdemo
After the project is created:
2. Introduce the first two classes. Because SWIFT is built, we first reference the OC file in the SWIFT project for a mixed compilation.
Use OC in swift
First, the header file and. M file are no longer used in swift. Therefore, you do not need to use import "" to import header files. Which Swift can access the OC class declaration.
In fact, swift also needs to use header files for access, but it does not need to use explicit import for import. There are two ways to generate this header file.
Method 1:In a brand new SWIFT, the bridge header file is automatically added with the first new prompt.
Click OK to generate a header file with <produceName-Bridging-Header.h>.
After the project is created:
One thing to note here is the bridging header file in targets-> build settings-> Object-C bridging header.
After completing the preceding steps, you can complete the bridge file.
Write the OC header file to be called in the swift class using import "to this bridge file. Like:
//// Use this file to import your target‘s public headers that you would like to expose to Swift.//MixDemo/MixDemo-Bridging-Header.h#import "OCChannel.h"
Similarly, when you know the relationship between this swift search header file, you don't need to pay attention to this-Bridging-Header.h file. You can create one manually and get your favorite name. For example:
Method 2:
Create a new header file named occontainerheader. h
Well, the above settings completely satisfy the class written by swift using oC.
Import uikitclass viewcontroller: uiviewcontroller {override func viewdidload () {super. viewdidload () // do any additional setup after loading the view, typically from a nib. // call the oc-class var channel = occhannel () println (channel. channelchange (10) println (channel. channelchange (2)} override func didreceivememorywarning () {super. didreceivememorywarning () // dispose of any resources that can be recreated .}}
Now let's take a look.
How does OC call the classes written by swift?. (In fact, if you copy my demo one by one, congratulations, you will not compile it above. Because my OC class references the class written by swfit, you must comment out the act class to run it .)
How does OC call the classes written by swift?
To use OC, you must have a header file. The swift file does not have a header file, so we must generate a header file. However, the header files that OC calls swift are special. Because the mechanism in the header file is automatically generated, you are not familiar with it and do not need to write it manually.
How to generate this header file. (Note that the header file set by the system cannot be seen in the project .)
Generation steps:
Select targets-> build settings-> packing-> product Module name to set the module name. The SWIFT header file is named based on this name.
Although you see this import "SwiftModule-swift.h" in the figure, you cannot find this file throughout the project, but you can use cmd + mouse to click to see the content in this header file.
In this way, if the project to check swift to use OC, the need to use the OC Class header file, all written in the MixDemo-Bridging-Header.h. Similarly, if the swift class used in OC, just need to clean one, then edit it, but do not forget to import the SwiftModule-swift.h Oh (name from, but-Swift. H is fixed.
Note:
If a class written in SWIFT is not followed by a derived class from nsobject or nsobject, no corresponding conversion class will be generated after compilation. So that the corresponding statement cannot be found in OC.
As in my example, class act won't be compiled into the SwiftModule-swift.h, but writing to class act: nsobject can compile the corresponding declaration.
Well, I will write it here today, and then study the mixed compilation between frameworks .....