UIApplication.sharedApplication().delegate作为AppDelegate导致EXC_BAD_ACCESS在快速单元测试中使用它 [英] UIApplication.sharedApplication().delegate as AppDelegate causes EXC_BAD_ACCESS using it on swift unit test

查看:88
本文介绍了UIApplication.sharedApplication().delegate作为AppDelegate导致EXC_BAD_ACCESS在快速单元测试中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试快速使用单元测试来测试一些实际的应用程序行为. 当我尝试从测试函数将de UIApplicationDelegate强制转换为我的AppDelegate时,出现了EXC_BAD_ACCESS异常.在测试代​​码下方:

I'm trying to use unit test in swift to test some of the real application behaviour. When i try to cast de UIApplicationDelegate to my AppDelegate from my test function i got and EXC_BAD_ACCESS exception. Below the test code:

func testGetAppDelegate(){

    let someDelegate = UIApplication.sharedApplication().delegate
    let appDelegate =  someDelegate as AppDelegate //EXC_BAD_ACCESS here
    XCTAssertNotNil(appDelegate, "failed to get cast pointer")
}

AppDelegate类设置为public,因此从访问级别上来看不是问题.

AppDelegate class is set to public so it is not a problem from access level.

在其工作的同一测试目标中使用Objective-C.在简单的指令下方:

Using objective-c in the same test target it works. Below the simple instruction:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

调试器说someDelegate是Builtin.RawPointer.不知道那是什么,我对低级细节不熟悉.

The debuger says someDelegate is a Builtin.RawPointer. Don't know what that is, i am not familiar with low level details.

推荐答案

我认为您在测试目标成员中添加了AppDelegate.swift.

I think you added AppDelegate.swift to the tests target members.

执行此操作时,AppName.AppDelegateAppNameTests.AppDelegate成为不同的类.然后,UIApplication.sharedApplication().delegate返回AppName.AppDelegate实例,但是您试图将其强制转换为AppNameTests.AppDelegate类型.这会导致EXC_BAD_ACCESS.

When you do that, AppName.AppDelegate and AppNameTests.AppDelegate becomes different classes. Then, UIApplication.sharedApplication().delegate returns AppName.AppDelegate instance, but you are trying to cast it to AppNameTests.AppDelegate type. That causes EXC_BAD_ACCESS.

相反,您必须从应用程序模块中导入它.

Instead of that, you have to import it from your application module.

import UIKit
import XCTest
import AppName // <- HERE

class AppNameTests: XCTestCase {
   // tests, tests...
}

并且,AppDelegate类及其方法和属性必须声明为public,以便在测试模块中可见.

And, AppDelegate class and its methods and properties must be declared as public to be visible from test module.

import UIKit

@UIApplicationMain
public class AppDelegate: UIResponder, UIApplicationDelegate {

    public var window: UIWindow?

    public func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

    // ...

同样,请确保从测试目标成员中删除AppDelegate.swift.

Again, Be sure to remove AppDelegate.swift from the Test target members.

这篇关于UIApplication.sharedApplication().delegate作为AppDelegate导致EXC_BAD_ACCESS在快速单元测试中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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