OC mixed with Swift, implementation of the three scenarios

Multi-language coexistence period, must be mixed into a way to achieve coexistence and OC Swift languages ​​in more than shadow turned out to be so simple

The first scenario, App achieved mixed

  1. Create a bridge file *.h
    a new bridge file, New File choose  Header File to create a
  2. Specifies the bridge file
    project->targets->Build Setting->All->Swift Compilter General->Objective-C-Bridging-Header to specify the new bridge just the file
    15675840517343041
  3. OC import header file ( *.h) to the bridge file
    such as:#import "Log.h"
    15675868286636003
  4. Complete the call
    in Swiftdirectly call the file

Note: Create a file for the first time in OC Swift project, Xcodeit will automatically be prompted to create a bridge file
15675668261700973

The second scene, Framework implemented in mixed

  1. Creating Famework
    Once created will generate a header file with the same name*.h
  2. The OC header ( *.h) as the public header file
    project->targets->Build Phases->Headers->Publicdirectly dragged
    15675857436917952
  3. OC import header file ( *.h) to a header file of the same name
    , such as:#import "Log.h" 15675870536575205
  4. Complete the call
    in external Swiftfiles called directly
    15675871376567946
    Note: external classes and methods must be  public marked before visit

The third scenario, Pod the package realized mixed

After completing the second scenario, add the *.podspecfiles to achieve  Pod package

  1. Generation  *.podspec
    to run the command  pod spec create [name] generates  *.podspec a file
  2. Editing  *.podspec
    in  *.podspec in the header file OC ( *.hset) discloses headers
    such as:spec.public_header_files = "Log.h"

     #完整的文件信息
     Pod::Spec.new do |spec|
         spec.name         = "OCSwiftFramework"
         spec.version      = "0.0.1"
         spec.summary      = "OCSwiftFramework"
         spec.description  = <<-DESC
                         OCSWiftFramework
                         DESC
         spec.homepage     = "https://www.ichochy.com"
    
         spec.license      = "MIT"
         # spec.license      = { :type => "MIT", :file => "FILE_LICENSE" }
         spec.author             = { "MLeo" => "[email protected]" }
    
         spec.platform = :osx
         spec.osx.deployment_target = "10.10"
    
         spec.source       = { :git => "https://github.com/iChochy/HelloWorld.git", :tag => "#{spec.version}" }
    
         spec.source_files  = "**/*.{h,m,swift}"
         spec.exclude_files = "Info.plist"
         spec.public_header_files = "Flog.h"
    
         spec.swift_version = "4.2"
     end
    
  3. Verify  *.podspec
    the Run command  pod lib lint to complete verification
  4. Complete call a. Run command  pod trunk push [NAME.podspec] to complete the publication
    b.  Podfile Add  pod "[name]"c. Run the command  pod install to complete the  pod installation

    . d After installation directly call
    Note: Available  pod "[name]", :path => "[path]" local designated

GitHub:

HelloWorld:https://github.com/iChochy/HelloWorld.git


Contact information:

E-mail: [email protected]
website: https://www.ichochy.com
source file: https://www.ichochy.com/blog/2019/09/04/OC and mixed Swift - achieve three scenarios way .html

Guess you like

Origin www.cnblogs.com/ichochy/p/11606079.html