Swift 2.0:使用Set时无法将MyApp.MyCustomClass值转换为MyAppTests.MyCustomClass [英] Swift 2.0: Could not cast value MyApp.MyCustomClass to MyAppTests.MyCustomClass when using Set

查看:107
本文介绍了Swift 2.0:使用Set时无法将MyApp.MyCustomClass值转换为MyAppTests.MyCustomClass的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个错误:


无法转换 MyApp.Member (0x1674daf8)到 MyAppTests.Member (0x4c07248)。

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

你可以轻松地重现错误:

You can reproduce a bug in easy way:


  1. 设置两个 NSManagedObject

@objc(Member)
class Member: NSManagedObject {
    @NSManaged var family: Family
}

@objc(Family)
class Family: NSManagedObject {
    @NSManaged var members: Set<Member>
}


  • .xcdatamodel中设置此项

    然后在 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)
    }
    



  • 推荐答案

    我找到了一个解决方案:

    我认为那些有这种问题的人会以这种方式将他们的文件导入测试目标:

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

    因为他们应该这样做:

    所以,只需删除文件来自你的测试目标。然后,如果您需要测试目标中的文件,只需在每个测试类中使用 @testable 关键字。

    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:-)

    Swift 2 + Xcode 7:单元测试访问变得轻松!!!!

    这篇关于Swift 2.0:使用Set时无法将MyApp.MyCustomClass值转换为MyAppTests.MyCustomClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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