Swift和Objective-C混編的Pod庫(kù)

背景

因?yàn)橹按罅康腛C代碼塊, 想要完全拋棄OC,有點(diǎn)不太現(xiàn)實(shí).
為了復(fù)用之前代碼庫(kù),采用了混編的模式

制作pod

  1. 命令創(chuàng)建pod庫(kù)
pod lib create ZHOCSwift //這里創(chuàng)建的是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' => 'zhangxi@xingshulin.com' }
  s.source           = { :git => 'https://github.com/zhangxi/ZHOCSwift.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  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. 添加文件
    image.png

    創(chuàng)建一個(gè)OC類(lèi) 一個(gè)swift類(lèi)
    注意: 類(lèi)要指定為public或open訪問(wèn)控制級(jí)別,不然外界無(wú)法訪問(wèn)
C.swift
A.m

結(jié)論

  1. C.swift里可直接調(diào)用OC的A類(lèi)
  2. swift里必須聲明為public并在需要OC調(diào)用的方法前加@objc
  3. OC類(lèi)調(diào)用swift需要導(dǎo)入#import <ZHOCSwift/ZHOCSwift-Swift.h>

!并沒(méi)有在swift工程中測(cè)試

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容