Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot access AppDelegate while testing Xcode project [duplicate]

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

The above function always crash and gives this error:

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

like image 595
Mohammed Avatar asked Oct 20 '22 08:10

Mohammed


1 Answers

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

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

@testable import MyAppName

and before XCode 7

import MyAppName
like image 50
Gerd Castan Avatar answered Oct 22 '22 01:10

Gerd Castan