背景
因?yàn)橹按罅康腛C代碼塊, 想要完全拋棄OC,有點(diǎn)不太現(xiàn)實(shí).
為了復(fù)用之前代碼庫(kù),采用了混編的模式
制作pod
- 命令創(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
- 添加文件
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é)論
- C.swift里可直接調(diào)用OC的A類(lèi)
- swift里必須聲明為public并在需要OC調(diào)用的方法前加@objc
- OC類(lèi)調(diào)用swift需要導(dǎo)入#import <ZHOCSwift/ZHOCSwift-Swift.h>
