After the swift language comes out, it may be possible for new projects to be developed directly using swift, but there may be situations in which some classes that have been written with OC or packaged modules are not ready to be written again in Swift, which uses a mash-up. This is allowed in the IOS8.
First of all, first of all, research in the same project directory under the mixed use situation.
For demonstration. Prepare two classes first
The first is the Swift language class, the file name is Act.swift
[CPP]View Plaincopy
- Import Foundation
- Class 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 with OC as OCChannel.h, implementing the file as OCCHANNEL.M
Header file
[CPP]View Plaincopy < param name= "wmode" value= "Transparent" >
- #import <Foundation/Foundation.h>
- @interface Occhannel:nsobject
- @property (Nonatomic,retain) NSString *channelname;
- -(NSString *) Channelchange: (Nsinteger) channels;
- @end
Implementation file
[CPP]View Plaincopy
- #import "OCChannel.h"
- #import "Swiftmodule-swift.h"
- @interface Occhannel ()
- {
- ACT *act; Class of//swift
- }
- @end
- @implementation Occhannel
- -(ID) init
- {
- if (self = [super init]) {
- NSLog (@"OC Constructor is called.");
- //Use 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 is a class act written by Swift. Mainly to demonstrate that in the same project, the Swift class calls OC, while the OC class also calls Swift. Thus forming a mixed-writing pattern.
Here are the specific steps:
1. Create a new Swift project: I'm here for the project named Mixdemo.
Built after the project:
2. Is the introduction of the previous two classes, we first to each one. Because it's swift, we'll start by referencing OC files in Swift Engineering
Using OC in Swift
First, the way the header files and. m files are no longer used in swift. Therefore, you do not need to import the header file using import "". How swift can access the class declaration of the OC.
In fact, Swift also needs to be accessed with a header file, but it is no longer necessary to import it explicitly using import. There are two ways to implement the generation of this header file.
mode one: in a new swift, the bridge header file is automatically added using the first new prompt.
Click OK this will generate a header file with <produceName-Bridging-Header.h>.
After the construction of the project:
Here is a place to note is the Targets->build settings->object-c bridging header is set to which bridge header file can be.
After the above steps, the bridge file is ready to be
Let's get the OC header file you want to call in the Swift class and use the import "" to write to this bridging file. Just like:
[CPP]View Plaincopy < param name= "wmode" value= "Transparent" >
- //
- Use the This file to import your target's public headers so would like to expose to Swift.
- Mixdemo/mixdemo-bridging-header.h
- #import "OCChannel.h"
Similarly, once you know the relationship of this swift search header file, you don't need to bother with this-bridging-header.h file. You can build one by hand and take your favorite name. Such as:
Way two:
Create a new header file named: OCContainerHeader.h
Well, the above setup will completely satisfy the class that Swift uses OC to write.
[CPP]View Plaincopy
- Import UIKit
- Class Viewcontroller:uiviewcontroller {
- Override Func Viewdidload () {
- Super.viewdidload ()
- additional setup after loading the view, typically from a nib.
- //Call OC class
- var channel = Occhannel ()
- println (channel. Channelchange (10))
- println (channel. Channelchange (2))
- }
- Override Func didreceivememorywarning () {
- Super.didreceivememorywarning ()
- //Dispose of any resources, can be recreated.
- }
- }
Okay, here's another look at how OC calls the Swift-written class . (In fact, if you are 1:1 copy me this demo, which congratulations you, in the above you will compile does not pass.) Because my OC class refers to a class written by Swfit, you have to comment on which act's class you want to run. )
How OC invokes Swift-written classes
To use OC, you must have a header file. The swift file does not have a header file, where we must also produce a header file. However, the header file for the OC call to Swift is quite special. Because the mechanism inside the header file is automatically generated, in unfamiliar, not recommended handwriting.
How to produce this header file. (Note that the system-Set header file is not visible in the project.) )
Production steps:
Select targets->build Settings->packing->product module name, which is important the name of the swift header is named after this.
Although you see this import "Swiftmodule-swift.h" in the picture, but you can not find this file in the entire project, but the use of cmd+ mouse click to see the contents of this header file.
In this way, in the case of Swift to use OC in the project, the need to use the OC class header file, all written in the mixdemo-bridging-header.h. Also if the use of the Swift class OC, only need to clean one, and then make up, but do not forget to import swiftmodule-swift.h Oh (name pick, but-swift.h is fixed), there is another need to pay attention to the reader.
Note:
If a class that is written with Swift does not follow a derived class from NSObject or NSObject, then the corresponding transformation class will not be generated after compilation. This makes it possible for OC to not find the corresponding declaration.
As in my example, class Act would not be compiled into Swiftmodule-swift.h, but written as Class Act:nsobject, you can compile the corresponding declaration. You can also use @objc to declare, but this is still the same, the class best inherit nsobject down. Just like the following:
[CPP]View Plaincopy
- Import Foundation
- @objc (ACT)
- Class Act
- {
- Func hasact (tag:int), String
- {
- switch (TAG)
- {
- Case 1:return "Movie"
- Case 2:return "CCTV"
- Case 3:return "Sport TV"
- default:return "area TV"
- }
- }
- @objc (init)//Originally thought to add this alloc can be found, but not ...
- Init ()
- {
- println ("Act constructor is called.")
- }
- Deinit
- {
- println ("Act destroyed is called.")
- }
- }
But when it comes to use, you'll find
act = [[Act Alloc]init]; Error, can not find alloc, it is recommended that everyone still inherit nsobject.
Well, write it up today and look at the mixing of frameworks .....
(OC and Swift Mixed)