当我尝试从我的测试函数将de UIApplicationDelegate投射到我的AppDelegate时,我得到了EXC_BAD_ACCESS异常.测试代码下方:
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,因此从访问级别来看不是问题.
在同一个测试目标中使用objective-c可行.简单说明下面:
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
deBUGer说someDelegate是一个Builtin.RawPointer.不知道那是什么,我不熟悉低级细节.
我认为您已将AppDelegate.swift添加到测试目标成员.当您这样做时,Appname.AppDelegate和AppnameTests.AppDelegate成为不同的类.然后,UIApplication.sharedApplication().delegate返回Appname.AppDelegate实例,但您尝试将其强制转换为AppnameTests.AppDelegate类型.这会导致EXC_BAD_ACCESS.
而不是那样,你必须从你的应用程序模块导入它.
import UIKitimport XCTestimport Appname // <- HEREclass AppnameTests: XCTestCase { // tests,tests...}
并且,必须将AppDelegate类及其方法和属性声明为public才能从测试模块中看到.
import UIKit@UIApplicationMainpublic 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 } // ...
同样,请务必从Test目标成员中删除AppDelegate.swift.
总结以上是内存溢出为你收集整理的单元测试 – UIApplication.sharedApplication().委托作为AppDelegate在swift单元测试中使用它导致EXC_BAD_ACCESS全部内容,希望文章能够帮你解决单元测试 – UIApplication.sharedApplication().委托作为AppDelegate在swift单元测试中使用它导致EXC_BAD_ACCESS所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)