我正在将应用程序从Ipad移植到mac.(我知道这听起来很奇怪)
我坚持使用NSScrollview.请指导我在NSScrollview中的contentize,contentOffset等效内容.
我想知道什么时候Swift对象的一组属性发生了变化.以前,我已经在Objective-C中实现了这个,但是我在将它转换为Swift时遇到了一些困难.
我以前的Objective-C代码是:
- (void) observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context {
if (![change[@"new"] isEqual:change[@"old"]])
[self edit];
}
Run Code Online (Sandbox Code Playgroud)
我在Swift解决方案中的第一次传递是:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if change?[.newKey] != change?[.oldKey] { // Compiler: "Binary operator '!=' cannot be applied to two 'Any?' operands"
edit()
}
}
Run Code Online (Sandbox Code Playgroud)
然而,编译器抱怨:"二元运算符'!='不能应用于两个'任何?' 操作数"
我的第二次尝试:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if let newValue = change?[.newKey] as? NSObject {
if let oldValue …
Run Code Online (Sandbox Code Playgroud)