Swift and mixed Objective-C

Swift wrote in a recent project, the learning process encountered some problems. First face is Swift and Objective-C mixed problems. Because bloggers with a time of Objective-C for a long time, and the current Swift tripartite library development time is shorter, and not so rich as Objective-C, so some of the tripartite library previous projects frequently used such as: MJRefresh, SDWebImage like a Swift did not find a good time to replace the version, so the Bowen recorded a number of issues some of Swift and Objective-C is mixed.

1. Edit the file Podfile

Use Xcode open Podfile file, join the tripartite library to be used, such as MJRefresh, then executepod install

      
      
1
2
      
      
use_frameworks!
the ' MJRefresh'

2. Build a bridge file

Simple approach is directly in the new Swift project file in Objective-C, Xcode will automatically prompt you to create a bridging headerbridge connector file, click Create Xcode automatically creates a file for you to bridge joints, and improve the bridge path. The new Objective-C files can be retained if necessary, without directly Move to Trash can.
bridging header

Of course, you can also directly commend + N to create a new Header Filenamed ProjectName-Bridging-Header.hand set up a bridge joints Building Settings file their own path
bridging header

3. Note

If you add in the Podfile file use_frameworks!, you can not import the header file Objective-C library tripartite joint in the bridge file, you can directly import files in use. Otherwise, you must import the file and place the bridge joint use of the tripartite library, the library must contain the tripartite folder name, for example:#import "MJRefresh/MJRefresh.h"

Objective-C calls Swift

1. Edit the file Podfile

Also you need to add tripartite libraries and use_frameworks in Podfile file!

      
      
1
2
      
      
use_frameworks!
pod ' Charts'

2. Import Swift Library Header Files

Xcode will automatically generate a header file for the Project in order to call in Objective-C.
Called Objective-C class Swift, just #import "moduleName-Swift.h"to call, header file provided with Xcode module name Swift code plus -Swift.h is named.
In the header file, containing the Swift Objective-C is supplied to all the interfaces, and macro definitions AppDelegate automatically generated

3. Note

moduleName-Swift.h is not searchable in Xcode, only click in from import into view.

In most cases, Objective-C can seamlessly call Swift, but because Swift relative to the Objective-C a few more new features, such as generics, tuples, enumerations, etc., so Swift storm drain to Objective- C interface more restrictions, thus exposing only effective in Swift Objective-C interfaces.

Original: Large columns  Swift and Objective-C mixed


Guess you like

Origin www.cnblogs.com/chinatrump/p/11597110.html