- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我试图通过用单个 switch 语句替换典型的长字符串嵌套 if/else 语句来提高我的 KVO observeValueForKeyPath 实现的易读性。
到目前为止,唯一真正起作用的是:
private let application = UIApplication.sharedApplication()
switch (object!, keyPath!) {
case let (object, "delegate") where object as? UIApplication === application:
appDelegate = application.delegate
break
...
default:
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
}
如果有的话,它比以下内容更难阅读:
if object as? UIApplication === application && keyPath! == "delegate" {
}
else {
}
有人有在observeValueForKeyPath
(和类似方法)中使用 switch 的好模型吗
编辑:与下面@critik 的问题相关,这里有更多代码来演示仅使用 switch (object as! NSObject, keyPath!){
的问题:
private let application = UIApplication.sharedApplication()
private var appDelegate : UIApplicationDelegate?
private var rootWindow : UIWindow?
public override func observeValueForKeyPath(
keyPath: String?,
ofObject object: AnyObject?,
change: [String : AnyObject]?,
context: UnsafeMutablePointer<Void>) {
switch (object as! NSObject, keyPath!) {
case (application, "delegate"):
appDelegate = application.delegate
(appDelegate as? NSObject)?.addObserver(self, forKeyPath: "window", options: [.Initial], context: nil)
break
case (appDelegate, "window"):
rootWindow = appDelegate?.window?.flatMap { $0 }
break
case (rootWindow, "rootViewController"):
rebuildViewControllerList(rootWindow?.rootViewController)
break
default:
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
}
}
最佳答案
元组上的开关
怎么样:
switch (object as! NSObject, keyPath!) {
case (application, "delegate"):
appDelegate = application.delegate
...
default:
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
}
注意 1. 虽然我再次反对 Swift 中的强制转换(向下转换、解包等),但您可以安全地强制向下转换为 NSObject
而不会发生崩溃,因为每this SO question , KVO 仅适用于 NSObject
子类。
注意 2. 在 Swift 中您也不需要 break
,这也会将您的代码至少缩短一行 :)
关于swift - 在 observeValueForKeyPath 中使用开关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34883367/
我正在使用观察者以下代码 myView = [[UIView alloc] initWithFrame:CGRectZero]; [myView addObserver:self forKeyPath
这是我第一次在 iOS 项目中使用 KVO,我想在通过 GPS 获取我的位置后观察我的两个 UILabel 的变化,并不断通知我。当我运行该项目时,我得到了 2 个显示日期的标签,它们随着时间而变化,
我正在开发一个测试应用程序,其中有一个 NSOperationQueue。我正在创建一个 NSInvocationOperation 并观察该操作的“isFinished”属性。奇怪的是,observ
observeValueForKeyPath 是否总是从主线程调用? 我正在记录通话 -(void) observeValueForKeyPath:(NSString *)keyPath
我有一个关于核心数据和通知的问题。看了 Apple 文档后,我没有找到答案。 问题是: 我有一个具有一对多关系的托管对象。我想知道什么时候从这个关系中添加或删除了一个对象。(“类别”有许多“项目”)我
我使用 KVO observe changes in a frame并相应地设置另一个框架 - (void)observeValueForKeyPath:(NSString *)keyPath ofO
我写了一个演示想尝试 KVO 编程,我找到了 observeValueForKeyPath当我只更改一次值时,方法总是执行两次,怎么了?请帮助我,谢谢 代码 状态同步器.h #import @in
这是我第一次使用 KVO,我马上就卡住了。问题是当调用 observeValueForKeyPath 时,我正在调用同一个类中的另一个方法。该方法只是显示一个警报 View 。我想的很简单,但警报 V
我在调用 observeValueForKeyPath 时遇到问题,但使用以下方法解决了它: observeValueForKeyPath not being called 不过,我想知道一般如何处理
我试图通过用单个 switch 语句替换典型的长字符串嵌套 if/else 语句来提高我的 KVO observeValueForKeyPath 实现的易读性。 到目前为止,唯一真正起作用的是: pr
我正在尝试使用 AVPlayer 创建一个视频播放器。从 youtube 加载视频。我希望当视频当前时间发生变化时,我的 uiSlider 和我的时间标签可以更新。我正在使用“observeValue
它是在属性更改后被调用(意味着属性 setter 已经从调用堆栈中弹出)还是在属性 setter 正在执行时被调用? Swift 有非常方便的 “Property observers”。 Obj-C
我已经使用乘数连接从循环中为连接的对等点实现了文件发送功能。即代码如下。 for connectedPeer in self.connectedPeers { let sendingPr
我有一个奇怪的问题。 我的类声明了一个只保留指向 C++ 对象的指针的属性: @property (assign) CPPObject *representedObject; 在那个 Obj-C 类的
我是 iOS 开发的新手,我想弄明白为什么我的应用程序会因 SIGABRT 消息而崩溃。我正在学习有关如何实现 Google Maps SDK 的教程 ( link),据我所知,我拥有相同的代码。 我
我有一个简单的 UIView 子项,我很困惑为什么会收到此错误(导致应用程序崩溃): [VideoView observeValueForKeyPath:ofObject:change:context
在this tutorial的帮助下我正在使用 Swift 向我的 View 添加一个进度条,但观察者没有收到任何自身调用。 // ViewController.swift import UIKit
我完全复制了 Apple documentation 中的示例使用以下方法观察键值: override func observeValueForKeyPath(keyPath: String?, o
我有一个 appDelegate,它有一个属性 myWindow of MyWindowClass。我需要从 myWindow 观察 bool 属性。比起我有一个 CustomViewControll
我想收到有关 NSMutableSet 中新插入的通知,因此这就是我正在做的事情,但出于某种原因,它没有调用 observeValueForKeyPath 方法 仅供测试: -(void)observ
我是一名优秀的程序员,十分优秀!