Objective
Swift has been in operation for several years, with Swift's language mechanism and ease of use more grounded than objective-c, greatly reducing the entry threshold for iOS. Of course, this is not the new children's shoes, is indeed the gospel, but for the entire iOS programming practitioners, it is really, once "tall", suddenly "short dwarfish poor." Plus training courses rampant, bulk wholesale, iOS can no longer see the glory of the year. IOS10 launch, followed by Xcode8 also pushed the update, careful people will find that Xcode8 iOS version of the minimum adaptation has become iOS8.0, coupled with the swift version to stabilize, in a sense, the swift era officially opened, instead of objective-c fear is only a matter of time. Of course, before we do that, we should also be prepared. The more companies that have come this year, the swift and Objective-c have started.
Let's take a look at some of the considerations and issues in the mix today:
Mixed
There are only two cases of mixing,
Documents using Swift in objective-c works or documents;
Use objective-c files in Swift Engineering or files.
The most important two files in the process of mixing:
1. Bridging files
The bridging file "Projectname-bridging-header.h" is automatically generated when other files are first created. If you accidentally delete, you can also manually add, but the name must be "projectname-bridging-header.h" header file (name composition: Project name-bridging-header.h), if the name is not clear, you can create a new header yourself After file, the Targets→build settings→swift compiler-general→objective-c Bridging Header profile path, which is primarily used by Swift when using the OC class.
2.objective-c Generated Interface Header name file
This file is mixed when the system generates a Swift file corresponding to the OBJECTIVE-C header file, which can be specified in Targets→build settings→swift compiler-general→objective-c Generated Interface Header name is configured, the default file name is the project name-swift.h, generally do not make changes.
Use Swift files in Objective-c Engineering or documents
When calling the classes in the swift file in the OC file, first add #import in the OC file "
Projectname-swift.h "(Name composition: Project name-swift)
Although this file is not visible in the project, but she really exists, after compiling, you can press and hold command+ click the file name, you will see the specific generated code.
After the introduction, the use of specific classes, directly in accordance with the OC way to use.
Use objective-c files in Swift Engineering or documents
When using OC files in Swift, simply introduce the required header file in the bridging file, the Projectname-bridging-header.h file.
Specific use, according to the corresponding SWIFT syntax structure can be.
Integrated considerations
For swift classes that need to be mixed add @objc declarations or subclasses that inherit NSObject or NSObject
class TestClass {// attribute // implementation }
If you want to use the TestClass class in the Objective-c class, you should declare it using @objc, or inherit testclass from NSObject or nsobject subclasses, otherwise After introducing Productname-swift.h, the program cannot find the corresponding class.
Using the third-party framework
Settings: Target-->build setting-->packaging-->defines module is "Yes";
Then, the configuration file target, Build phases, Link Binary, adds the framework to import;
Finally, it is necessary to configure the bridge file, such as to use the abc.h in the Abc-lib.framework Library to configure: #import "abc-lib/abc.h";
Subclass sub-Class problems
For a custom class, the class of objective-c cannot inherit from Swift's class, that is, the OC class to be mixed cannot be a subclass of the Swift class. In turn, the Swift class that needs to be mixed can inherit from the OC class. Annotations
OC Macro File
If you want to use macros defined in OC for swift files, you can use only constant simple macro files.
Swift's unique features
There are many features of the OC that are not available in swift, such as Swift has tuples, functions for class-one citizens, and unique enumeration types. Therefore, the mixed file to be used should be aware of Swift's unique property issues.
Block with OC in the case of Swift
Using closure in Swift cannot use block as an attribute to pass a value, it must be an initialization method or function.
In the Objective-c file:
#import <UIKit/UIKit.h>void (^myblock) (NSString *@interface ////-(void) TransValue: (myblock) block; @end
The following is the. m file
#import " FirViewController.h " @implementation -(void) viewdidload { [super Viewdidload]; =-(void) TransValue: (myblock) block{ if (block) { block (@ "firback"); @end
Callback in Swift file:
When Swift uses the OC class, first declare the OC header file in the bridging file
Project name-bridging-header.h This is the case when you create a swift project
Import UIKitclassViewcontroller:uiviewcontroller {Overridefunc viewdidload () {super.viewdidload () Self.view.backgroundColor=uicolor.whitecolor ()} @IBOutlet weak var Gofirst:uibutton!@IBAction func gofirstaction (sender:anyobject) {Let Firvc:firviewcontroller=Firviewcontroller () firvc. TransValue {(arg:string!) -Voidinchself.abtn?. Settitle (ARG, ForState:UIControlState.Normal)} Self.navigationcontroller?. Pushviewcontroller (FIRVC, animated:true) }
Swift and objective-c language of the mixed, I hope the great God to give valuable advice! (@。 Ε. @)
Swift and OBJECTIVE-C Integrated considerations