Swift: Can't cast ViewController returned by instantiateInitialViewController()
| Originator: | andrewhalls.galtsoft | ||
| Number: | rdar://18043164 | Date Originated: | 17-Aug-2014 |
| Status: | Closed | Resolved: | Programmer Error |
| Product: | iOS SDK | Product Version: | iOS 8, Beta 5 |
| Classification: | Bug | Reproducible: | Yes |
Summary:
I'm trying to test a ViewController in Swift. I'm using the
Steps to Reproduce:
Start with a fresh project, chose iOS Application -> Single View Application template, Swift, Universal
Replaced the testExample with the code as shown below, Added ViewController to the test target. Run the tests ...
func testExample() {
var storyBoard: UIStoryboard?
var anyVC: AnyObject?
var viewController: ViewController?
storyBoard = UIStoryboard(name:"Main", bundle: nil)
XCTAssert(storyBoard != nil, "Test Not Configured Properly")
anyVC = storyBoard?.instantiateInitialViewController()
viewController = anyVC as? ViewController
XCTAssert(viewController != nil, "Test Not Configured Properly")
// This is an example of a functional test case.
XCTAssert(true, "Pass")
}
Note I can force the cast ... with the following code
anyVC = storyBoard?.instantiateInitialViewController()
But then it crashes with this dump:
libswiftCore.dylib`swift_dynamicCastClassUnconditional:
0x10724f5a0: pushq %rbp
0x10724f5a1: movq %rsp, %rbp
0x10724f5a4: pushq %r14
0x10724f5a6: pushq %rbx
0x10724f5a7: movq %rsi, %rbx
0x10724f5aa: movq %rdi, %r14
0x10724f5ad: testq %r14, %r14
0x10724f5b0: je 0x10724f5de ; swift_dynamicCastClassUnconditional + 62
0x10724f5b2: movabsq $-0x7fffffffffffffff, %rax
0x10724f5bc: andq %r14, %rax
0x10724f5bf: jne 0x10724f5de ; swift_dynamicCastClassUnconditional + 62
0x10724f5c1: movq %r14, %rdi
0x10724f5c4: callq 0x107279a6e ; symbol stub for: object_getClass
0x10724f5c9: nopl (%rax)
0x10724f5d0: cmpq %rbx, %rax
0x10724f5d3: je 0x10724f5ed ; swift_dynamicCastClassUnconditional + 77
0x10724f5d5: movq 0x8(%rax), %rax
0x10724f5d9: testq %rax, %rax
0x10724f5dc: jne 0x10724f5d0 ; swift_dynamicCastClassUnconditional + 48
0x10724f5de: leaq 0x3364d(%rip), %rax ; "Swift dynamic cast failed"
0x10724f5e5: movq %rax, 0xa456c(%rip) ; gCRAnnotations + 8
0x10724f5ec: int3
0x10724f5ed: movq %r14, %rax
0x10724f5f0: popq %rbx
0x10724f5f1: popq %r14
0x10724f5f3: popq %rbp
0x10724f5f4: retq
0x10724f5f5: nopw %cs:(%rax,%rax)
Expected Results:
Expect the test to pass.
Actual Results:
Test fails, with nil value.
Version:
Xcode 6 Beta 5, Swift,
Notes:
Stackoverflow discussion:
http://stackoverflow.com/questions/25345010/cant-cast-custom-uiviewcontroller-from-uistoryboard-in-xctest
Configuration:
iPhone5s Simulator, iOS8
Attachments:
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!
Operator error
The test and the app are in different modules, using namesapce sysntax fixes the issue.