在测试Xcode项目时无法访问AppDelegate [英] Cannot access AppDelegate while testing Xcode project

查看:108
本文介绍了在测试Xcode项目时无法访问AppDelegate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

func testExample() {
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    // some code ...
}

以上功能总是如此崩溃并发出此错误:

The above function always crash and gives this error:


无法将MyAppName.AppDelegate类型的值(0x10dc09e80)转换为MyAppNameTests.AppDelegate(0x11cc190c0) )。

Could not cast value of type 'MyAppName.AppDelegate' (0x10dc09e80) to 'MyAppNameTests.AppDelegate' (0x11cc190c0).


推荐答案

您可能正在将AppDelegate编译到测试目标中。不要这样做。

You are probably compiling AppDelegate into your test target. Don't do this.

相反,只能编译到您的普通应用目标MyAppName。在你的测试类中写入XCode 7

Instead, only compile into your normal app target MyAppName. In you test class write in XCode 7

@testable import MyAppName

在XCode 7之前

import MyAppName

这篇关于在测试Xcode项目时无法访问AppDelegate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆