Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 测试时,默认undoManager为零_Ios_Swift_Unit Testing_Nsundomanager - Fatal编程技术网

Ios 测试时,默认undoManager为零

Ios 测试时,默认undoManager为零,ios,swift,unit-testing,nsundomanager,Ios,Swift,Unit Testing,Nsundomanager,我正在为使用undoManager的Swift库添加一些测试。在库的演示中,将以下打印添加到viewdide:将导致打印NSUndoManagerObject override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) print(undoManager) } 我为测试编写了另一个ViewController,当我在ViewController的viewdide:中有相同的代码时,将打印

我正在为使用
undoManager
的Swift库添加一些测试。在库的演示中,将以下打印添加到
viewdide:
将导致打印
NSUndoManagerObject

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    print(undoManager)
}
我为测试编写了另一个ViewController,当我在ViewController的
viewdide:
中有相同的代码时,将打印
nil

我使用以下代码为测试创建ViewController:

override func setUp() {
    super.setUp()

    // Put setup code here. This method is called before the invocation of each test method in the class.
    let storyboard = UIStoryboard(name: "Main", bundle: NSBundle(forClass: self.dynamicType))

    viewController = storyboard.instantiateInitialViewController() as! ViewController
    viewController.viewDidLoad()
    viewController.viewDidAppear(false)

    // Test and Load the View at the Same Time!
    XCTAssertNotNil(viewController.view)
}
我在ViewController的
viewDidLoad:
中调用
becomeFirstResponder()

我想测试使用默认
undoManager
的逻辑,所以我需要弄清楚为什么它是
nil
。我已尝试为默认的
undoManager
分配
NSUndoManager
的新实例,但它是只读属性


如果您对代码感兴趣,请看下面的示例。

我无法使用默认的
undoManager
来进行此项目的测试,但我最终使用了以下解决方案,我认为它同样有效。在使用
undoManager
的类中,我添加了一个私有
NSUndoManager
变量,然后在
init
中定义它,如下所示:

classUndoManager = undoManager
if classUndoManager == nil {
    classUndoManager = NSUndoManager()
}

任何使用过
undoManager
的地方现在都使用
classUndoManager

调用
loadView
之前的
viewDidLoad
。@nhgrif只是尝试了一下,运气不好:(无论如何,谢谢!