Attention to Swift and objective-c.2016-10-19 13:29 Edit: An Immortal Light lamp Category: iOS development Source: Rembean's book 0
Ocswift
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 for 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.
The past is no longer mentioned, the matter is still to be done. 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,
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 consists of: 工程名-Bridging-Header.h ), if the name is not clear can also create a new header 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 swift files corresponding to the OBJECTIVE-C header file, which can be configured in the Targets-->Build Settings-->Swift Compiler - General-->Objective-C Generated Interface Header Name configuration, the default file name is 工程名-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.
Mixed considerations Add a @objc declaration or subclass of NSObject or NSObject to a swift class that needs to be mixed
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 typedef void (^myblock) (NSString *arg); @interface Firviewcontroller:uiviewcontroller//@property (copy, nonatomic) Myblock Myblock; This form of public parameters is problematic if the callback is removed in the Swift class. Tip There is no initialization method, so use the following method with block as the parameter-(void) TransValue: (myblock) block; @end
The following is the. m file
#import "FirViewController.h" @implementation Firviewcontroller-(void) viewdidload {[Super viewdidload]; Self.view.backgroundColor = [Uicolor Whitecolor]; }-(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 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