This article is taken from: http://blog.csdn.net/mmoaay/article/details/48422309
This article is based on Xcode 6.4 and Swift 1.2
Important information
Using Swift with Cocoa and Objective-C official documentation
Why mix it up?
Language development trend (TIOBE), Swift rankings continue to rise, OC rankings show a gravitational decline
Required for normal project iteration
Many third-party libraries are still implemented using OC
If the module that has been implemented in OC in the project is rewritten in Swift, the cost is slightly higher
We need to use Swift in the project to really encounter problems and solve problems
Note: It is not mixed for the purpose of mixing. Mixed compilation is only a relatively reasonable choice made after comprehensive measurement of development resources, project management and technological development trends.
How to start mixing? step
To create a project, Language can choose Swift or Objective-C.
Create Swift file and add bridging header file
When adding a Swift file, Xcode will automatically prompt you to add a bridging header file, select Yes
Make two key settings
These two settings Xcode will be set by default, you can change the Objective-C Bridging Header and Objective-C Generated Interface Header Name to the name you want to set.
At this point, even if the configuration of the Swift and Objective-C mixed environment is complete.
XXX-Bridging-Header.h
If you need to use OC code or library in Swift, you only need to import the corresponding code or library header file in this file.
XXX-Swift.h
Unlike XXX-Bridging-Header.h, the XXX-Swift.h file does not appear in the project, but is automatically generated by Xcode. You can find the XXX-Swift.h file of the corresponding project under the path similar to the following: ( PS: I did not write it in the PPT during the speech, I am really sorry)
/Users/perry/Library/Developer/Xcode/DerivedData/XXX-bhlzdinkujybftbjmgwjwclndmss/Build/Intermediates/XXX.build/Debug-iphonesimulator/XXX.build/Objects-normal/x86_64/XXX-Swift.h
1
If you need to use Swift code in OC, #import XXX-Swift.h in the file used (PS: some other considerations for using Swift code in OC will be described in detail later)
View the code in the XXX-Swift.h file:
It is not difficult to find that the content in this file is actually the code in Swift converted into the code of OC.
Note: If the project is cleaned up, this file will also be deleted, and during the rebuilding process, this file will only be regenerated if all Swift code has been compiled.
Combined with framework
Complicated macro in pit tread time OC
The Swift compiler does not include a preprocessor. Instead, it makes full use of compile-time attributes, generation configuration, and language features to accomplish the same function. Therefore, for the above-mentioned similar macro definitions, it is recommended to use the method to repackage once.
Macros in OC have the same name as classes in Swift
Because Swift cannot use #define, and OC can, you may define a macro in OC that has the same name as Swift. If you have never used the functions provided by Swift code in OC (that is, never #import XXX -Swift.h), there will be no problems when compiling, but if it is used, it will report the following error:
Here is because I use #define MView (@ "MView") to define MView as a macro of type NSString, and in Swift MView is a subclass of UIView. After #import "MDemo-Swift.h", MView is The definition is repeated, resulting in an error.
Update: If the class in OC has the same name as the class in Swift, this also applies.
Swift uses enums in OC
If you just use the value, you can directly use the enumeration
If you need to operate on enumeration values, you need to use .value
OC uses enums in Swift
Need to add @objc modifier before Swift enum definition
The type of enumeration must be Int
@objc / dynamic / NS *
If the Swift class needs to be used in OC, it is recommended to inherit the NS * series of classes
If a member or method in the Swift class needs to be used in OC, use the @objc modifier
If members in the Swift class need to support KVC / KVO, use the dynamic modifier
IBOutlet vs IBOutletCollection
There is no IBOutletCollection in Swift, but the IBOutletCollection is implemented as follows:
@IBOutlet var labels: [UILabel]!
1
The situation of this IBOutlet in xib / Storyboard is as follows:
It should be noted that unlike OC's IBOutletCollection, the elements in labels are not necessarily arranged in the order of black, white, blue, green!
Repeated inclusion
In OC, you may encounter the case where the type A header file needs to include the type B header file, and the type B header file also needs to include the type A header file. At this time, use @class to solve the problem. But when the class A header file in the OC needs to include XXX-Swift.h, and the class A header file is imported in XXX-Bridging-Header.h, this is more embarrassing. See the example below:
The situation in the above example is this:
First, we have two Swift classes MManager and MData. An OC ViewController.
Then, a method in the MManager class needs to pass in the handle of the ViewController class instance, and then assign an MData class instance to the member variable mData in the ViewController class instance (the type of this mData is MData).
In this scenario, there will be repeated inclusion, because there is already a condition in the MManager class that needs to pass in the handle of the ViewController class instance, so we must #import "ViewController in XXX-Bridging-Header.h .h ", so we can only avoid #include" XXX-Swift.h "by going to #import" XXX-Swift.h "in the ViewController.m file. At this time, we need to define the member variable mData as the id type, and then cast it to the MData type when it is specifically used in the ViewController.m file. as follows:
You can solve the problem of repeated inclusion.
The problem with this approach is that the member variable mData can accept any type, so when we use it, we need to determine whether mData is the data type we need. Here we introduce how to determine the id of AnyObject and OC in Swift :
OC:
if ([self.mData isKindOfClass: [MData class]]) {// If self.mData is not of type MData, the judgment is unsuccessful
}
1
2
Swift:
if let data = self.mData as? MData {// If self.mData is not of type MData, the judgment is unsuccessful
}
1
2
properties
If the OC class is imported into Swift, the properties of the class will change as follows:
The properties in Swift are noatomic, so atomic in OC class will be invalid
The getter / setter methods overridden in the OC class will be invalid
Hello! Swift 2 var-> let
Compared with Swift 1.2, Swift 2 strongly requires that the value that will not change in this method body be declared as a constant, otherwise warning will occur, so when writing Swift 1.2 code, you can pay attention to this in advance, thereby reducing conversion costs .
println
This method has been removed in Swift 2 and should not be used.
do / while-> repeat / while
Because there are changes, it is recommended to use for / for… in instead
Protocol-oriented programming
Starting from Swift 2, we can extend the protocol, and officially opened the era of protocol-oriented programming in Swift. Because it has not been specifically used in the project, the research is not very deep. You can refer to the following three translations:
Swift 2: Protocol-oriented programming
How to use the protocol correctly
Protocol-oriented MVVM in Swift 2.0
The way to mix Objective-C and Swift