Swift 2.0: Could not cast value MyApp.MyCustomClass to MyAppTests.MyCustomClass when using Set

谁都会走 提交于 2019-12-18 03:02:05

问题


This is an error:

Could not cast value of type MyApp.Member (0x1674daf8) to MyAppTests.Member (0x4c07248).

You can reproduce a bug in easy way:

  1. Setup two NSManagedObject

    @objc(Member)
    class Member: NSManagedObject {
        @NSManaged var family: Family
    }
    
    @objc(Family)
    class Family: NSManagedObject {
        @NSManaged var members: Set<Member>
    }
    
  2. Setup this also in your .xcdatamodel:

  3. Then in your TestFile:

    func testA() {
    
        let family = Family.MR_createEntityInContext(context)
        let father = Member.MR_createEntityInContext(context)
    
        father.family = family
    
        let firstMember = family.members.first
    
        XCTAssertEqual(firstMember!, father)
    }
    

回答1:


I have found a solution:

I suppose that those who has such problem, imported their files into test target this way:

Since they should do it just like this:

So, just remove the files from your test target. And then if you need your files within test target just use @testable keyword within your every test class.

This way there is no problem with casting values between targets anymore. It worked for me:-) Enjoy:-)

Read more from Swift 2 + Xcode 7: Unit Testing Access Made Easy!!!!



来源:https://stackoverflow.com/questions/31089389/swift-2-0-could-not-cast-value-myapp-mycustomclass-to-myapptests-mycustomclass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!