Swift and objective-c Mixed considerations:
Objective
Swift, which has been in operation for several years, has significantly reduced the entry threshold for iOS compared with the objective-c of Swift's language mechanism and ease of use. Of course, this for the new children's shoes did not say, is indeed the gospel, but for the entire iOS programming practitioners, really, once upon a time, "tall", "short dwarfish poor". Plus training courses rampant, bulk wholesale, iOS no longer see the glory of the year. IOS10 launched, followed by the Xcode8 also pushed the update, the attentive will find that Xcode8 iOS version of the minimum adaptation has become iOS8.0, and the swift version tends to stabilize, in a sense, the era of Swift officially opened, instead of objective-c fear is only a matter of time. Of course, before we do, we should be prepared. The more companies that have come this year, the more the swift and the Objective-c have begun to mix.
Let's take a look at some of the considerations and issues in the mix:
Mixed
Mixing is nothing but two situations,
Use Swift's documents in objective-c works or documents;
Use objective-c files in Swift Engineering or documentation.
The two most important documents in the process of mixing:
1. Bridging file
The bridging file "Projectname-bridging-header.h" is automatically generated when additional files are created for the first time. 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 After file, in the Targets→build settings→swift compiler-general→objective-c Bridging header configuration file path, this file 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 the 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 filename is the project name-swift.h, generally do not make changes.
Documents using Swift in OBJECTIVE-C Works or documents
When invoking a class in the swift file in the OC file, first add #import to the OC file.
Projectname-swift.h "(Name Composition: Engineering name-swift)
Although this file is not visible in the project, but she is real, after compiling, you can hold down 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 documentation
When you use the OC file in Swift, you simply introduce the required header file into the bridging file, the Projectname-bridging-header.h file.
Use it, according to the corresponding SWIFT syntax structure.
Mixed points of attention
Add @objc declaration or inherit nsobject or NSObject subclass for swift classes that need to be mixed
Class TestClass {
//attribute
//implementation
}
If you want to use the TestClass class in the Objective-c class, you should use @objc to declare it, or you will inherit testclass from the NSObject or nsobject subclasses, otherwise After the introduction of Productname-swift.h, the program could not find the corresponding class.
Using the third party framework
Setting: Target-->build setting-->packaging-->defines module is "Yes";
Then, the configuration file target-> build phases-> Link Binary to add the framework to import;
Finally, or to configure the bridge files, such as to use the Abc-lib.framework library in the abc.h will be configured like this: #import "abc-lib/abc.h";
Subclass of the child class
For a custom class, a Objective-c class cannot inherit from the Swift class, that is, the OC class to be mixed cannot be a subclass of the Swift class. Conversely, a mixed swift class can inherit from the OC class. Annotations
OC Macro File
If the swift file is to use a macro defined in OC, you can use only the constant simple macro file.
Swift Unique features
Swift has many features that OC does not have, for example, Swift has tuples, functions for first-class citizens, and unique enumeration types. Therefore, the mixed files to be used should pay attention to the problem of Swift's unique attributes.
The case of Swift uses the OC block
Use of closure in Swift cannot be used as a property to pass a value, it must be an initialization method or function.
Objective-c file:
#import <UIKit/UIKit.h>
typedef void (^myblock) (NSString *arg);
@interface Firviewcontroller:uiviewcontroller
//@property (copy, nonatomic) Myblock Myblock;
This form of public argument, if invoked in the swift category, is problematic. Tip There is no initialization method, so use the following method with block as the parameter
-(void) TransValue: (myblock) block;
@end
Here is the. m file
#import "FirViewController.h"
@implementation firviewcontroller
-(void) viewdidload
{
[super Viewdidload];
Self.view.backgroundColor = [Uicolor whitecolor];
}
-(void) TransValue: (myblock) block
{
if (blocks)
{block
(@ "Firback");
}
@end
In Swift file callback:
When Swift uses the class of OC, first declare the header file of OC in the bridging file
Project name-bridging-header.h This is the case of creating the Swift project
Import Uikit
class Viewcontroller:uiviewcontroller
{
override func 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!)-> Void in
self.abtn? Settitle (ARG, forState:UIControlState.Normal)
}
Self.navigationcontroller? Pushviewcontroller (FIRVC, animated:true)
}
Swift and Objective-c language mix so much, hope that the great God to give valuable advice! (@。 Ε. @)
Thank you for reading, I hope to help you, thank you for your support for this site!