Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

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

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)
    }
    
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have found a solution:

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

enter image description here

Since they should do it just like this:

enter image description here

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.

enter image description here

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!!!!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...