我的自定义库中有一个变量,设置该变量后,应在MainViewController中更新该变量。
我已经进行了“属性观察者”和“键值观察”,但是我认为这两种情况都不适合我的情况。
Add a property observer on an existing property in an iOS library class-这不起作用。
正如@Alexander建议的那样,使用Combine框架可以解决此问题。我完成了本教程:https://benoitpasquier.com/first-steps-apple-combine-framework-swift/,但是有没有扩展的简便方法。
//CustomLibClassLibrary Library
class CustomLibClass {
public var String1 : String = ""
func ReceivedData(data: String) {
String1 = data
}
}
//Application
import CustomLibClassLibrary
class MainViewController: NSViewController {
var appString : String = ""
@IBOutlet weak var uiLabel: NSTextView!
func displayString {
uiLabel.string = appString
}
}
设置了“ String1”时,变量“ appString”应更新。
答案 0 :(得分:0)
尝试从CustomLibClass继承新类并在其中创建代理
import Combine
import SwiftUI
import UIKit
import CustomLibClassLibrary
class CustomLibClassSuccessor: CustomLibClass, ObservableObject {
override var String1: String {
didSet {
proxyString = String1
}
}
@Published var proxyString: String = ""
}
class TestViewController: UIViewController {
@ObservedObject var object = CustomLibClassSuccessor()
private var disposables = Set<AnyCancellable>()
override func viewDidLoad() {
super.viewDidLoad()
let objectObs =
object.$proxyString.sink { proxyString in
print("\(proxyString)")
}
disposables.insert(objectObs)
}
}