Swift和Objective-C混编的Pod库

背景

因为之前大量的OC代码块, 想要完全抛弃OC,有点不太现实.
为了复用之前代码库,采用了混编的模式

制作pod

  1. 命令创建pod库
pod lib create ZHOCSwift //这里创建的是OBJC的工程

2.podspec

Pod::Spec.new do |s|
  s.name             = 'ZHOCSwift'
  s.version          = '0.1.0'
  s.summary          = 'A short description of ZHOCSwift.'

# This description is used to generate tags and improve search results.
#   * Think: What does it do? Why did you write it? What is the focus?
#   * Try to keep it short, snappy and to the point.
#   * Write the description between the DESC delimiters below.
#   * Finally, don't worry about the indent, CocoaPods strips it!

  s.description      = <<-DESC
TODO: Add long description of the pod here.
                       DESC

  s.homepage         = 'https://github.com/zhangxi/ZHOCSwift'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'zhangxi' => '[email protected]' }
  s.source           = { :git => 'https://github.com/zhangxi/ZHOCSwift.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/'

  s.ios.deployment_target = '8.0'

s.source_files = 'ZHOCSwift/Classes/**/*.{h,m,swift}'
  
  # s.resource_bundles = {
  #   'ZHOCSwift' => ['ZHOCSwift/Assets/*.png']
  # }

  s.public_header_files = 'ZHOCSwift/Classes/**/*.h'
  s.pod_target_xcconfig = { 'SWIFT_VERSION' => '4.0' }
  # s.frameworks = 'UIKit', 'MapKit'
  # s.dependency 'AFNetworking', '~> 4.0'
end
  1. 添加文件
    Swift和Objective-C混编的Pod库_第1张图片
    image.png

    创建一个OC类 一个swift类
    注意: 类要指定为public或open访问控制级别,不然外界无法访问
Swift和Objective-C混编的Pod库_第2张图片
C.swift
Swift和Objective-C混编的Pod库_第3张图片
A.m

结论

  1. C.swift里可直接调用OC的A类
  2. swift里必须声明为public并在需要OC调用的方法前加@objc
  3. OC类调用swift需要导入#import

!并没有在swift工程中测试

你可能感兴趣的:(Swift和Objective-C混编的Pod库)