我下载了两个完全不同的pod,将它们导入到我的项目中,在我的一个视图控制器中使用它们,如果我选择使用其中任何一个,一切正常。
但是,如果我尝试从其中一个pod文件中访问同一个视图控制器,则无法识别相同的视图控制器。我还创建并尝试向视图控制器发送通知,但通知没有响应(它工作正常,我从我创建的其他类中尝试过)。然后我在pod文件的类下创建了一个单例,然后尝试访问单例,但没有发生任何事情(应该运行print语句)。
它发生在2个不同的pod文件中,它们都工作正常,所以我假设还有另一个问题我忽略了防止外部文件在pod中工作?
pod在MyController中运行良好
import ThePod
class MyController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(printSomethingInMyController(_:)), name: Notification.Name("printSomethingInMyController"), object: nil)
// the pod works fine
let podFile = FileWithinThePod()
}
@IBAction func buttonTapped(_ sender: UIButton) {
// the pod does what it's supposed to do
podFile.startSomeAction()
}
@objc fileprivate func printSomethingInMyController(_ notification: Notification) {
print("notification- this should print in MyController")
}
static func printSomethingElse() {
print("this a class print function")
}
}
在pod文件中无法访问MyController
open class FileWithinThePod {
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
setUp() {
// whatever this file needs
}
func startSomeAction() {
// 0. the pod does something and it works fine
// 1. ***THE PROBLEM IS HERE. I can't access MyController (photo below)
MyController.printSomethingElse()
// 2. ***THE PROBLEM IS ALSO HERE. This notification never fires because nothing ever prints
NotificationCenter.default.post(name: Notification.Name("printSomethingInMyController"), object: nil, userInfo: nil)
// 3. *** nothing happens with MySingleton because nothing ever prints
MySingleton.startPrinting()
// 4. *** same thing nothing prints
let x = MySingleton.sharedInstance
x.tryPrintingAgain()
}
}
class MySingleton {
static let sharedInstance = MySingleton()
static func startPrinting() {
print("print something from MySingleton")
NotificationCenter.default.post(name: Notification.Name("printSomethingInMyController"), object: nil, userInfo: nil)
}
func tryPrintingAgain() {
print("try again")
NotificationCenter.default.post(name: Notification.Name("printSomethingInMyController"), object: nil, userInfo: nil)
}
}
这是一种理想的行为。 pod文件(库)不依赖于应用程序目标或应用程序类。它对您的文件或类没有任何了解。
您的应用程序取决于那些库和那些不依赖于您的应用程序的库。像这样编辑图书馆并不是一件好事,因为在接下来的pod update
上,这些变化可能会消失。
解决方案:将pod项目中的源文件添加到应用程序文件夹中。不要将它们添加为pod。