Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

objective c - Swift dynamic cast failed - Error when trying to run unit tests

Xcode Version 6.1.1 (6A2008a)

Error: libswiftCore.dylib`swift_dynamicCastClassUnconditional and Swift dynamic cast failed

I am trying to test and I am coming across this error that I am unsure about how to fix. Can someone please shed some light on this?

Error happens:

The error happens with this cast:

var delegate = UIApplication.sharedApplication().delegate as AppDelegate

After reading this post: I deleted the AppDelegate.swift from the test target but then I get an error when building as the compiler complains about not knowing about "AppDelegate" in the cast. I am confused?!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok so after a lot of messing about here is what solved the issue:

  1. I removed all the test target membership from the files in the main (non-test) part of my application, including the AppDelegate.

enter image description here

  1. I changed all the classes (in the main part of the application) that I needed to access (from the Test part of the application) to be public and also set these class's required methods to be public as well).

  2. I imported the main project into each appropriate test classes with an #import AppName statement

Hope that saves some other people some time.. :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...